{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "rstan (Version 2.19.3, GitRev: 2e1f913d3ca3)\n",
      "\n",
      "For execution on a local, multicore CPU with excess RAM we recommend calling\n",
      "options(mc.cores = parallel::detectCores()).\n",
      "To avoid recompilation of unchanged Stan programs, we recommend calling\n",
      "rstan_options(auto_write = TRUE)\n",
      "\n",
      "Loading required package: zoo\n",
      "\n",
      "\n",
      "Attaching package: ‘zoo’\n",
      "\n",
      "\n",
      "The following objects are masked from ‘package:base’:\n",
      "\n",
      "    as.Date, as.Date.numeric\n",
      "\n",
      "\n",
      "Loading required package: lubridate\n",
      "\n",
      "\n",
      "Attaching package: ‘lubridate’\n",
      "\n",
      "\n",
      "The following object is masked from ‘package:base’:\n",
      "\n",
      "    date\n",
      "\n",
      "\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "[1] ‘2.19.3’"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "[1] ‘2.21.0.1’"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "'2.21.0'"
      ],
      "text/latex": [
       "'2.21.0'"
      ],
      "text/markdown": [
       "'2.21.0'"
      ],
      "text/plain": [
       "[1] \"2.21.0\""
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "libraries = c(\"dplyr\",\"magrittr\",\"tidyr\",\"ggplot2\",\"rstan\",\"readxl\")\n",
    "for(x in libraries) { library(x,character.only=TRUE,warn.conflicts=FALSE,quietly=TRUE) }\n",
    "\n",
    "require(zoo)\n",
    "require(lubridate)\n",
    "\n",
    "base_sz = 12 # base_size parameter\n",
    "theme_set(theme_bw())\n",
    "\n",
    "'%&%' = function(x,y) paste0(x,y)\n",
    "\n",
    "options(mc.cores = parallel::detectCores())\n",
    "rstan_options(auto_write = TRUE)\n",
    "\n",
    "packageVersion(\"rstan\")\n",
    "packageVersion(\"StanHeaders\")\n",
    "rstan::stan_version()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "filenames = c(\"data_incper\", \"data_ons_hosp\", \"dthdata_hosp_dth\", \"dthdata_ons_dth\", \"dthdata_ons_hosp\", \"data_incper_inclwuhan\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# <font color=\"purple\">Without truncation for all datasets except *data_incper_inclwuhan* dataset</font>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## <font color=\"orange\">Lognormal distribution</font>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "for (idx in 1:(length(filenames)-1)) \n",
    "{\n",
    "    ## main dir for Stan simulations\n",
    "    standirname = 'stan-sims/'%&%filenames[idx]%&%\"-lognormal-no_truncation\"\n",
    "    unlink(standirname, recursive=T)\n",
    "    dir.create(standirname)\n",
    "\n",
    "    datafilename = \"../../data/\"%&%filenames[idx]%&%\".csv\"\n",
    "    read.table(datafilename, sep=\",\", header=T) %>% select(EL,ER,SL,SR,tstar) %>% mutate(dist = (SR+SL)/2-(ER+EL)/2) -> df\n",
    "\n",
    "    # Dumping data\n",
    "    N = nrow(df)\n",
    "    E_L = df$EL\n",
    "    E_R = df$ER\n",
    "    S_L = df$SL\n",
    "    S_R = df$SR\n",
    "    stan_rdump(c('E_L', 'E_R', 'S_L', 'S_R', 'N'), file=standirname%&%\"/Data.R\") \n",
    "\n",
    "    # Dumping initial conditions\n",
    "    e_raw = rep(.5, N)\n",
    "    s_raw = rep(.5, N)\n",
    "    logmean_SI = log(mean(df$dist))\n",
    "    logsd_SI = log(sd(df$dist))\n",
    "    stan_rdump(c('e_raw', 's_raw', 'logmean_SI', 'logsd_SI'), file=standirname%&%\"/Init.R\")  \n",
    "\n",
    "    # Stan program\n",
    "    \"data {\n",
    "        int<lower = 0> N; // number of records\n",
    "        vector<lower = 0>[N] E_L;\n",
    "        vector<lower = 0>[N] E_R;\n",
    "        vector<lower = 0>[N] S_L;\n",
    "        vector<lower = 0>[N] S_R;\n",
    "    }\n",
    "\n",
    "    parameters {\n",
    "        real logmean_SI;\n",
    "        real logsd_SI;\n",
    "\n",
    "        vector<lower = 0, upper = 1>[N] s_raw;\n",
    "        vector<lower = 0, upper = 1>[N] e_raw;\n",
    "    }\n",
    "\n",
    "    transformed parameters {\n",
    "        real<lower = 0> param2 = sqrt(log((exp(2*(logsd_SI-logmean_SI))+1.0)));\n",
    "        real param1 = logmean_SI - param2^2/2.0;\n",
    "\n",
    "        vector<lower = min(S_L), upper = max(S_R)>[N] s;\n",
    "        vector<lower = min(E_L), upper = max(E_R)>[N] e;\n",
    "\n",
    "        s = S_L + (S_R - S_L) .* s_raw;\n",
    "        for (k in 1:N) {\n",
    "            if (E_R[k] > s[k]) \n",
    "                e[k] = E_L[k] + (s[k] - E_L[k]) * e_raw[k];\n",
    "            else\n",
    "                e[k] = E_L[k] + (E_R[k] - E_L[k]) * e_raw[k];\n",
    "        }\n",
    "    }\n",
    "\n",
    "    model {\n",
    "        logmean_SI ~ std_normal();\n",
    "        logsd_SI ~ std_normal();\n",
    "\n",
    "        e_raw ~ normal(0.5, 1.0);\n",
    "        s_raw ~ normal(0.5, 1.0);\n",
    "\n",
    "        for (k in 1:N) \n",
    "            target += lognormal_lpdf(s[k] - e[k] | param1, param2);\n",
    "    }\n",
    "\n",
    "    generated quantities {\n",
    "        real<lower = 0> mean_SI = exp(param1 + param2^2/2);\n",
    "        real<lower = 0> sd_SI = sqrt((exp(param2^2)-1)*exp(2*param1+param2^2));\n",
    "\n",
    "        vector[N] log_likelihood;\n",
    "        for (k in 1:N) \n",
    "            log_likelihood[k] = lognormal_lpdf(s[k] - e[k] | param1, param2);\n",
    "    }\" %>% cat(file=standirname %&% \"/fit.stan\", sep=\"\", fill=TRUE)\n",
    "\n",
    "    standistribdir = \"../../../CmdStan\"\n",
    "    stanscriptdir = \"../Hokkaido_Wuhan_IncubationPeriod_2020/scripts/Andrei-more_accurate/\"%&%standirname\n",
    "    ## bash file\n",
    "    \"#!/bin/bash\n",
    "    cwd=$(pwd)\n",
    "    cd \"%&%standistribdir%&%\"\n",
    "    make -j6 \"%&%stanscriptdir%&%\"/fit\n",
    "    cd \"%&%stanscriptdir%&%\"\n",
    "    mkdir -p diagnostics\n",
    "    for i in {1..4}\n",
    "    do\n",
    "        echo Running ${i}\n",
    "        SEEDNUMBER=$((1+$i))\n",
    "        ./fit \\\\\n",
    "            method=sample num_samples=25000 num_warmup=100000 save_warmup=0 \\\\\n",
    "                adapt delta=0.98 \\\\\n",
    "                algorithm=hmc \\\\\n",
    "                    engine=nuts \\\\\n",
    "            random seed=${SEEDNUMBER} \\\\\n",
    "            id=$i \\\\\n",
    "            data file=Data.R \\\\\n",
    "            init=Init.R \\\\\n",
    "            output file=trace-$i.csv \\\\\n",
    "                diagnostic_file=diagnostics/diagnostics-$i.csv > diagnostics/output-$i.txt &\n",
    "    done\n",
    "    echo Finished sampling haha!\n",
    "    \" %>% cat(file=standirname%&%\"/fit.sh\", sep=\"\", fill=TRUE)\n",
    "\n",
    "    ## running the bash script\n",
    "    system(\"bash \"%&%standirname%&%\"/fit.sh\", intern = TRUE)\n",
    "}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## <font color=\"orange\">Gamma distribution</font>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "for (idx in 1:(length(filenames)-1)) \n",
    "{\n",
    "    ## main dir for Stan simulations\n",
    "    standirname = 'stan-sims/'%&%filenames[idx]%&%\"-gamma-no_truncation\"\n",
    "    unlink(standirname, recursive=T)\n",
    "    dir.create(standirname)\n",
    "\n",
    "    datafilename = \"../../data/\"%&%filenames[idx]%&%\".csv\"\n",
    "    read.table(datafilename, sep=\",\", header=T) %>% select(EL,ER,SL,SR,tstar) %>% mutate(dist = (SR+SL)/2-(ER+EL)/2) -> df\n",
    "\n",
    "    # Dumping data\n",
    "    N = nrow(df)\n",
    "    E_L = df$EL\n",
    "    E_R = df$ER\n",
    "    S_L = df$SL\n",
    "    S_R = df$SR\n",
    "    stan_rdump(c('E_L', 'E_R', 'S_L', 'S_R', 'N'), file=standirname%&%\"/Data.R\") \n",
    "\n",
    "    # Dumping initial conditions\n",
    "    e_raw = rep(.2, N)\n",
    "    s_raw = rep(.8, N)\n",
    "    mean_SI = mean(df$dist)\n",
    "    sd_SI = sd(df$dist)\n",
    "    stan_rdump(c('e_raw', 's_raw', 'mean_SI', 'sd_SI'), file=standirname%&%\"/Init.R\")  \n",
    "\n",
    "    # Stan program\n",
    "    \"data {\n",
    "        int<lower = 0> N; // number of records\n",
    "        vector<lower = 0>[N] E_L;\n",
    "        vector<lower = 0>[N] E_R;\n",
    "        vector<lower = 0>[N] S_L;\n",
    "        vector<lower = 0>[N] S_R;\n",
    "    }\n",
    "\n",
    "    parameters {\n",
    "        real<lower=0> mean_SI;\n",
    "        real<lower=0> sd_SI;\n",
    "\n",
    "        vector<lower = 0, upper = 1>[N] e_raw;\n",
    "        vector<lower = 0, upper = 1>[N] s_raw;\n",
    "    }\n",
    "\n",
    "    transformed parameters {\n",
    "        real<lower = 0> param1 = (mean_SI/sd_SI)^2;\n",
    "        real<lower = 0> param2 = mean_SI/(sd_SI^2);\n",
    "\n",
    "        vector<lower = min(S_L), upper = max(S_R)>[N] s;\n",
    "        vector<lower = min(E_L), upper = max(E_R)>[N] e;\n",
    "\n",
    "        s = S_L + (S_R - S_L) .* s_raw;\n",
    "        for (k in 1:N) {\n",
    "            if (E_R[k] > s[k]) \n",
    "                e[k] = E_L[k] + (s[k] - E_L[k]) * e_raw[k];\n",
    "            else\n",
    "                e[k] = E_L[k] + (E_R[k] - E_L[k]) * e_raw[k];\n",
    "        }\n",
    "    }\n",
    "\n",
    "    model {\n",
    "        mean_SI ~ normal(5.0, 10.0);\n",
    "        sd_SI ~ cauchy(0, 5.0);\n",
    "\n",
    "        e_raw ~ normal(0.5, 1.0);\n",
    "        s_raw ~ normal(0.5, 1.0);\n",
    "\n",
    "        for (k in 1:N) \n",
    "            target += gamma_lpdf(s[k] - e[k] | param1, param2);\n",
    "    }\n",
    "\n",
    "    generated quantities {\n",
    "        vector[N] log_likelihood;\n",
    "        for (k in 1:N) \n",
    "            log_likelihood[k] = gamma_lpdf(s[k] - e[k] | param1, param2);\n",
    "    }\" %>% cat(file=standirname %&% \"/fit.stan\", sep=\"\", fill=TRUE)\n",
    "\n",
    "    standistribdir = \"../../../CmdStan\"\n",
    "    stanscriptdir = \"../Hokkaido_Wuhan_IncubationPeriod_2020/scripts/Andrei-more_accurate/\"%&%standirname\n",
    "    ## bash file\n",
    "    \"#!/bin/bash\n",
    "    cwd=$(pwd)\n",
    "    cd \"%&%standistribdir%&%\"\n",
    "    make -j6 \"%&%stanscriptdir%&%\"/fit\n",
    "    cd \"%&%stanscriptdir%&%\"\n",
    "    mkdir -p diagnostics\n",
    "    for i in {1..4}\n",
    "    do\n",
    "        echo Running ${i}\n",
    "        SEEDNUMBER=$((1+$i))\n",
    "        ./fit \\\\\n",
    "            method=sample num_samples=25000 num_warmup=100000 save_warmup=0 \\\\\n",
    "                adapt delta=0.98 \\\\\n",
    "                algorithm=hmc \\\\\n",
    "                    engine=nuts \\\\\n",
    "            random seed=${SEEDNUMBER} \\\\\n",
    "            id=$i \\\\\n",
    "            data file=Data.R \\\\\n",
    "            init=Init.R \\\\\n",
    "            output file=trace-$i.csv \\\\\n",
    "                diagnostic_file=diagnostics/diagnostics-$i.csv > diagnostics/output-$i.txt &\n",
    "    done\n",
    "    echo Finished sampling haha!\n",
    "    \" %>% cat(file=standirname%&%\"/fit.sh\", sep=\"\", fill=TRUE)\n",
    "\n",
    "    ## running the bash script\n",
    "    system(\"bash \"%&%standirname%&%\"/fit.sh\", intern = TRUE)\n",
    "}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## <font color=\"orange\">Weibull distribution</font>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "for (idx in 1:(length(filenames)-1)) \n",
    "{\n",
    "    ## main dir for Stan simulations\n",
    "    standirname = 'stan-sims/'%&%filenames[idx]%&%\"-weibull-no_truncation\"\n",
    "    unlink(standirname, recursive=T)\n",
    "    dir.create(standirname)\n",
    "\n",
    "    datafilename = \"../../data/\"%&%filenames[idx]%&%\".csv\"\n",
    "    read.table(datafilename, sep=\",\", header=T) %>% select(EL,ER,SL,SR,tstar) %>% mutate(dist = (SR+SL)/2-(ER+EL)/2) -> df\n",
    "\n",
    "    # Dumping data\n",
    "    N = nrow(df)\n",
    "    E_L = df$EL\n",
    "    E_R = df$ER\n",
    "    S_L = df$SL\n",
    "    S_R = df$SR\n",
    "    stan_rdump(c('E_L', 'E_R', 'S_L', 'S_R', 'N'), file=standirname%&%\"/Data.R\") \n",
    "\n",
    "    # Dumping initial conditions\n",
    "    e_raw = rep(.2, N)\n",
    "    s_raw = rep(.8, N)\n",
    "    mean_SI = mean(df$dist)\n",
    "    param1 = 1.75\n",
    "    stan_rdump(c('e_raw', 's_raw', 'mean_SI', 'param1'), file=standirname%&%\"/Init.R\") \n",
    "\n",
    "    # Stan program\n",
    "    \"data {\n",
    "        int<lower = 0> N; // number of records\n",
    "        vector<lower = 0>[N] E_L;\n",
    "        vector<lower = 0>[N] E_R;\n",
    "        vector<lower = 0>[N] S_L;\n",
    "        vector<lower = 0>[N] S_R;\n",
    "    }\n",
    "\n",
    "    parameters {\n",
    "        real<lower = 0> mean_SI;\n",
    "        real<lower = 0> param1;\n",
    "\n",
    "        vector<lower = 0, upper = 1>[N] e_raw;\n",
    "        vector<lower = 0, upper = 1>[N] s_raw;\n",
    "    }\n",
    "\n",
    "    transformed parameters {\n",
    "        real<lower = 0> param2 = mean_SI/tgamma(1.0+1.0/param1);\n",
    "\n",
    "        vector<lower = min(S_L), upper = max(S_R)>[N] s;\n",
    "        vector<lower = min(E_L), upper = max(E_R)>[N] e;\n",
    "\n",
    "        s = S_L + (S_R - S_L) .* s_raw;\n",
    "        for (k in 1:N) {\n",
    "            if (E_R[k] > s[k]) \n",
    "                e[k] = E_L[k] + (s[k] - E_L[k]) * e_raw[k];\n",
    "            else\n",
    "                e[k] = E_L[k] + (E_R[k] - E_L[k]) * e_raw[k];\n",
    "        }\n",
    "    }\n",
    "\n",
    "    model {\n",
    "        mean_SI ~ normal(5.0, 10.0);\n",
    "        param1 ~ exponential(0.0001);\n",
    "\n",
    "        e_raw ~ normal(0.5, 1.0);\n",
    "        s_raw ~ normal(0.5, 1.0);\n",
    "\n",
    "        for (k in 1:N) \n",
    "            target += weibull_lpdf(s[k] - e[k] | param1, param2);\n",
    "    }\n",
    "\n",
    "    generated quantities {\n",
    "        real sd_SI = param2*sqrt(tgamma(1.0+2.0/param1)-(tgamma(1.0+1.0/param1))^2);\n",
    "\n",
    "        vector[N] log_likelihood;\n",
    "        for (k in 1:N) \n",
    "            log_likelihood[k] = weibull_lpdf(s[k] - t[k] | param1, param2);\n",
    "    }\" %>% cat(file=standirname %&% \"/fit.stan\", sep=\"\", fill=TRUE)\n",
    "\n",
    "    standistribdir = \"../../../CmdStan\"\n",
    "    stanscriptdir = \"../Hokkaido_Wuhan_IncubationPeriod_2020/scripts/Andrei-more_accurate/\"%&%standirname\n",
    "    ## bash file\n",
    "    \"#!/bin/bash\n",
    "    cwd=$(pwd)\n",
    "    cd \"%&%standistribdir%&%\"\n",
    "    make -j6 \"%&%stanscriptdir%&%\"/fit\n",
    "    cd \"%&%stanscriptdir%&%\"\n",
    "    mkdir -p diagnostics\n",
    "    for i in {1..4}\n",
    "    do\n",
    "        echo Running ${i}\n",
    "        SEEDNUMBER=$((1+$i))\n",
    "        ./fit \\\\\n",
    "            method=sample num_samples=25000 num_warmup=100000 save_warmup=0 \\\\\n",
    "                adapt delta=0.98 \\\\\n",
    "                algorithm=hmc \\\\\n",
    "                    engine=nuts \\\\\n",
    "            random seed=${SEEDNUMBER} \\\\\n",
    "            id=$i \\\\\n",
    "            data file=Data.R \\\\\n",
    "            init=Init.R \\\\\n",
    "            output file=trace-$i.csv \\\\\n",
    "                diagnostic_file=diagnostics/diagnostics-$i.csv > diagnostics/output-$i.txt &\n",
    "    done\n",
    "    echo Finished sampling haha!\n",
    "    \" %>% cat(file=standirname%&%\"/fit.sh\", sep=\"\", fill=TRUE)\n",
    "\n",
    "    ## running the bash script\n",
    "    system(\"bash \"%&%standirname%&%\"/fit.sh\", intern = TRUE)\n",
    "}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# <font color=\"purple\">Without truncation for *data_incper_inclwuhan* dataset</font>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## <font color=\"orange\">Lognormal distribution</font>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<style>\n",
       ".list-inline {list-style: none; margin:0; padding: 0}\n",
       ".list-inline>li {display: inline-block}\n",
       ".list-inline>li:not(:last-child)::after {content: \"\\00b7\"; padding: 0 .5ex}\n",
       "</style>\n",
       "<ol class=list-inline><li>''</li><li>'--- Translating Stan model to C++ code ---'</li><li><span style=white-space:pre-wrap>'bin/stanc  --o=../Hokkaido_Wuhan_IncubationPeriod_2020/scripts/Andrei-more_accurate/stan-sims/data_incper_inclwuhan-lognormal-no_truncation/fit.hpp ../Hokkaido_Wuhan_IncubationPeriod_2020/scripts/Andrei-more_accurate/stan-sims/data_incper_inclwuhan-lognormal-no_truncation/fit.stan'</span></li><li><span style=white-space:pre-wrap>'mpicxx -fvisibility-inlines-hidden -std=c++17 -fmessage-length=0 -march=nocona -mtune=haswell -ftree-vectorize -fPIC -fstack-protector-strong -fno-plt -O2 -ffunction-sections -pipe -isystem /home/aakhmetz/anaconda3/include -std=c++1y -D_REENTRANT -Wno-sign-compare    -Wno-delete-non-virtual-dtor  -I stan/lib/stan_math/lib/tbb_2019_U8/include -O3 -I src -I stan/src -I lib/rapidjson_1.1.0/ -I stan/lib/stan_math/ -I stan/lib/stan_math/lib/eigen_3.3.3 -I stan/lib/stan_math/lib/boost_1.72.0 -I stan/lib/stan_math/lib/sundials_4.1.0/include -DNDEBUG -D_FORTIFY_SOURCE=2 -O2 -isystem /home/aakhmetz/anaconda3/include    -DBOOST_DISABLE_ASSERTS   -DSTAN_MPI   -c -MT ../Hokkaido_Wuhan_IncubationPeriod_2020/scripts/Andrei-more_accurate/stan-sims/data_incper_inclwuhan-lognormal-no_truncation/fit.o -MT ../Hokkaido_Wuhan_IncubationPeriod_2020/scripts/Andrei-more_accurate/stan-sims/data_incper_inclwuhan-lognormal-no_truncation/fit -include ../Hokkaido_Wuhan_IncubationPeriod_2020/scripts/Andrei-more_accurate/stan-sims/data_incper_inclwuhan-lognormal-no_truncation/fit.hpp -include src/cmdstan/main.cpp -MM -E -MG -MP -MF ../Hokkaido_Wuhan_IncubationPeriod_2020/scripts/Andrei-more_accurate/stan-sims/data_incper_inclwuhan-lognormal-no_truncation/fit.d ../Hokkaido_Wuhan_IncubationPeriod_2020/scripts/Andrei-more_accurate/stan-sims/data_incper_inclwuhan-lognormal-no_truncation/fit.hpp'</span></li><li>''</li><li>'--- Compiling, linking C++ code ---'</li><li><span style=white-space:pre-wrap>'mpicxx -fvisibility-inlines-hidden -std=c++17 -fmessage-length=0 -march=nocona -mtune=haswell -ftree-vectorize -fPIC -fstack-protector-strong -fno-plt -O2 -ffunction-sections -pipe -isystem /home/aakhmetz/anaconda3/include -std=c++1y -D_REENTRANT -Wno-sign-compare    -Wno-delete-non-virtual-dtor  -I stan/lib/stan_math/lib/tbb_2019_U8/include -O3 -I src -I stan/src -I lib/rapidjson_1.1.0/ -I stan/lib/stan_math/ -I stan/lib/stan_math/lib/eigen_3.3.3 -I stan/lib/stan_math/lib/boost_1.72.0 -I stan/lib/stan_math/lib/sundials_4.1.0/include -DNDEBUG -D_FORTIFY_SOURCE=2 -O2 -isystem /home/aakhmetz/anaconda3/include    -DBOOST_DISABLE_ASSERTS   -DSTAN_MPI   -c  -x c++ -o ../Hokkaido_Wuhan_IncubationPeriod_2020/scripts/Andrei-more_accurate/stan-sims/data_incper_inclwuhan-lognormal-no_truncation/fit.o ../Hokkaido_Wuhan_IncubationPeriod_2020/scripts/Andrei-more_accurate/stan-sims/data_incper_inclwuhan-lognormal-no_truncation/fit.hpp'</span></li><li><span style=white-space:pre-wrap>'mpicxx -fvisibility-inlines-hidden -std=c++17 -fmessage-length=0 -march=nocona -mtune=haswell -ftree-vectorize -fPIC -fstack-protector-strong -fno-plt -O2 -ffunction-sections -pipe -isystem /home/aakhmetz/anaconda3/include -std=c++1y -D_REENTRANT -Wno-sign-compare    -Wno-delete-non-virtual-dtor  -I stan/lib/stan_math/lib/tbb_2019_U8/include -O3 -I src -I stan/src -I lib/rapidjson_1.1.0/ -I stan/lib/stan_math/ -I stan/lib/stan_math/lib/eigen_3.3.3 -I stan/lib/stan_math/lib/boost_1.72.0 -I stan/lib/stan_math/lib/sundials_4.1.0/include -DNDEBUG -D_FORTIFY_SOURCE=2 -O2 -isystem /home/aakhmetz/anaconda3/include    -DBOOST_DISABLE_ASSERTS   -DSTAN_MPI  -Wl,-O2 -Wl,--sort-common -Wl,--as-needed -Wl,-z,relro -Wl,-z,now -Wl,--disable-new-dtags -Wl,--gc-sections -Wl,-rpath,/home/aakhmetz/anaconda3/lib -Wl,-rpath-link,/home/aakhmetz/anaconda3/lib -L/home/aakhmetz/anaconda3/lib       -Wl,-L,\"/home/aakhmetz/Dropbox/CmdStan/stan/lib/stan_math/lib/boost_1.72.0/stage/lib\" -Wl,-rpath,\"/home/aakhmetz/Dropbox/CmdStan/stan/lib/stan_math/lib/boost_1.72.0/stage/lib\" -Wl,-L,\"/home/aakhmetz/Dropbox/CmdStan/stan/lib/stan_math/lib/tbb\" -Wl,-rpath,\"/home/aakhmetz/Dropbox/CmdStan/stan/lib/stan_math/lib/tbb\"  ../Hokkaido_Wuhan_IncubationPeriod_2020/scripts/Andrei-more_accurate/stan-sims/data_incper_inclwuhan-lognormal-no_truncation/fit.o src/cmdstan/main.o         stan/lib/stan_math/lib/sundials_4.1.0/lib/libsundials_nvecserial.a stan/lib/stan_math/lib/sundials_4.1.0/lib/libsundials_cvodes.a stan/lib/stan_math/lib/sundials_4.1.0/lib/libsundials_idas.a stan/lib/stan_math/lib/sundials_4.1.0/lib/libsundials_kinsol.a stan/lib/stan_math/lib/boost_1.72.0/stage/lib/libboost_serialization.so stan/lib/stan_math/lib/boost_1.72.0/stage/lib/libboost_mpi.so stan/lib/stan_math/stan/math/prim/functor/mpi_cluster_inst.o stan/lib/stan_math/lib/tbb/libtbb.so.2 -o ../Hokkaido_Wuhan_IncubationPeriod_2020/scripts/Andrei-more_accurate/stan-sims/data_incper_inclwuhan-lognormal-no_truncation/fit'</span></li><li>'Running 1'</li><li>'Running 2'</li><li>'Running 3'</li><li>'Running 4'</li><li>'Finished sampling haha!'</li></ol>\n"
      ],
      "text/latex": [
       "\\begin{enumerate*}\n",
       "\\item ''\n",
       "\\item '--- Translating Stan model to C++ code ---'\n",
       "\\item 'bin/stanc  --o=../Hokkaido\\_Wuhan\\_IncubationPeriod\\_2020/scripts/Andrei-more\\_accurate/stan-sims/data\\_incper\\_inclwuhan-lognormal-no\\_truncation/fit.hpp ../Hokkaido\\_Wuhan\\_IncubationPeriod\\_2020/scripts/Andrei-more\\_accurate/stan-sims/data\\_incper\\_inclwuhan-lognormal-no\\_truncation/fit.stan'\n",
       "\\item 'mpicxx -fvisibility-inlines-hidden -std=c++17 -fmessage-length=0 -march=nocona -mtune=haswell -ftree-vectorize -fPIC -fstack-protector-strong -fno-plt -O2 -ffunction-sections -pipe -isystem /home/aakhmetz/anaconda3/include -std=c++1y -D\\_REENTRANT -Wno-sign-compare    -Wno-delete-non-virtual-dtor  -I stan/lib/stan\\_math/lib/tbb\\_2019\\_U8/include -O3 -I src -I stan/src -I lib/rapidjson\\_1.1.0/ -I stan/lib/stan\\_math/ -I stan/lib/stan\\_math/lib/eigen\\_3.3.3 -I stan/lib/stan\\_math/lib/boost\\_1.72.0 -I stan/lib/stan\\_math/lib/sundials\\_4.1.0/include -DNDEBUG -D\\_FORTIFY\\_SOURCE=2 -O2 -isystem /home/aakhmetz/anaconda3/include    -DBOOST\\_DISABLE\\_ASSERTS   -DSTAN\\_MPI   -c -MT ../Hokkaido\\_Wuhan\\_IncubationPeriod\\_2020/scripts/Andrei-more\\_accurate/stan-sims/data\\_incper\\_inclwuhan-lognormal-no\\_truncation/fit.o -MT ../Hokkaido\\_Wuhan\\_IncubationPeriod\\_2020/scripts/Andrei-more\\_accurate/stan-sims/data\\_incper\\_inclwuhan-lognormal-no\\_truncation/fit -include ../Hokkaido\\_Wuhan\\_IncubationPeriod\\_2020/scripts/Andrei-more\\_accurate/stan-sims/data\\_incper\\_inclwuhan-lognormal-no\\_truncation/fit.hpp -include src/cmdstan/main.cpp -MM -E -MG -MP -MF ../Hokkaido\\_Wuhan\\_IncubationPeriod\\_2020/scripts/Andrei-more\\_accurate/stan-sims/data\\_incper\\_inclwuhan-lognormal-no\\_truncation/fit.d ../Hokkaido\\_Wuhan\\_IncubationPeriod\\_2020/scripts/Andrei-more\\_accurate/stan-sims/data\\_incper\\_inclwuhan-lognormal-no\\_truncation/fit.hpp'\n",
       "\\item ''\n",
       "\\item '--- Compiling, linking C++ code ---'\n",
       "\\item 'mpicxx -fvisibility-inlines-hidden -std=c++17 -fmessage-length=0 -march=nocona -mtune=haswell -ftree-vectorize -fPIC -fstack-protector-strong -fno-plt -O2 -ffunction-sections -pipe -isystem /home/aakhmetz/anaconda3/include -std=c++1y -D\\_REENTRANT -Wno-sign-compare    -Wno-delete-non-virtual-dtor  -I stan/lib/stan\\_math/lib/tbb\\_2019\\_U8/include -O3 -I src -I stan/src -I lib/rapidjson\\_1.1.0/ -I stan/lib/stan\\_math/ -I stan/lib/stan\\_math/lib/eigen\\_3.3.3 -I stan/lib/stan\\_math/lib/boost\\_1.72.0 -I stan/lib/stan\\_math/lib/sundials\\_4.1.0/include -DNDEBUG -D\\_FORTIFY\\_SOURCE=2 -O2 -isystem /home/aakhmetz/anaconda3/include    -DBOOST\\_DISABLE\\_ASSERTS   -DSTAN\\_MPI   -c  -x c++ -o ../Hokkaido\\_Wuhan\\_IncubationPeriod\\_2020/scripts/Andrei-more\\_accurate/stan-sims/data\\_incper\\_inclwuhan-lognormal-no\\_truncation/fit.o ../Hokkaido\\_Wuhan\\_IncubationPeriod\\_2020/scripts/Andrei-more\\_accurate/stan-sims/data\\_incper\\_inclwuhan-lognormal-no\\_truncation/fit.hpp'\n",
       "\\item 'mpicxx -fvisibility-inlines-hidden -std=c++17 -fmessage-length=0 -march=nocona -mtune=haswell -ftree-vectorize -fPIC -fstack-protector-strong -fno-plt -O2 -ffunction-sections -pipe -isystem /home/aakhmetz/anaconda3/include -std=c++1y -D\\_REENTRANT -Wno-sign-compare    -Wno-delete-non-virtual-dtor  -I stan/lib/stan\\_math/lib/tbb\\_2019\\_U8/include -O3 -I src -I stan/src -I lib/rapidjson\\_1.1.0/ -I stan/lib/stan\\_math/ -I stan/lib/stan\\_math/lib/eigen\\_3.3.3 -I stan/lib/stan\\_math/lib/boost\\_1.72.0 -I stan/lib/stan\\_math/lib/sundials\\_4.1.0/include -DNDEBUG -D\\_FORTIFY\\_SOURCE=2 -O2 -isystem /home/aakhmetz/anaconda3/include    -DBOOST\\_DISABLE\\_ASSERTS   -DSTAN\\_MPI  -Wl,-O2 -Wl,--sort-common -Wl,--as-needed -Wl,-z,relro -Wl,-z,now -Wl,--disable-new-dtags -Wl,--gc-sections -Wl,-rpath,/home/aakhmetz/anaconda3/lib -Wl,-rpath-link,/home/aakhmetz/anaconda3/lib -L/home/aakhmetz/anaconda3/lib       -Wl,-L,\"/home/aakhmetz/Dropbox/CmdStan/stan/lib/stan\\_math/lib/boost\\_1.72.0/stage/lib\" -Wl,-rpath,\"/home/aakhmetz/Dropbox/CmdStan/stan/lib/stan\\_math/lib/boost\\_1.72.0/stage/lib\" -Wl,-L,\"/home/aakhmetz/Dropbox/CmdStan/stan/lib/stan\\_math/lib/tbb\" -Wl,-rpath,\"/home/aakhmetz/Dropbox/CmdStan/stan/lib/stan\\_math/lib/tbb\"  ../Hokkaido\\_Wuhan\\_IncubationPeriod\\_2020/scripts/Andrei-more\\_accurate/stan-sims/data\\_incper\\_inclwuhan-lognormal-no\\_truncation/fit.o src/cmdstan/main.o         stan/lib/stan\\_math/lib/sundials\\_4.1.0/lib/libsundials\\_nvecserial.a stan/lib/stan\\_math/lib/sundials\\_4.1.0/lib/libsundials\\_cvodes.a stan/lib/stan\\_math/lib/sundials\\_4.1.0/lib/libsundials\\_idas.a stan/lib/stan\\_math/lib/sundials\\_4.1.0/lib/libsundials\\_kinsol.a stan/lib/stan\\_math/lib/boost\\_1.72.0/stage/lib/libboost\\_serialization.so stan/lib/stan\\_math/lib/boost\\_1.72.0/stage/lib/libboost\\_mpi.so stan/lib/stan\\_math/stan/math/prim/functor/mpi\\_cluster\\_inst.o stan/lib/stan\\_math/lib/tbb/libtbb.so.2 -o ../Hokkaido\\_Wuhan\\_IncubationPeriod\\_2020/scripts/Andrei-more\\_accurate/stan-sims/data\\_incper\\_inclwuhan-lognormal-no\\_truncation/fit'\n",
       "\\item 'Running 1'\n",
       "\\item 'Running 2'\n",
       "\\item 'Running 3'\n",
       "\\item 'Running 4'\n",
       "\\item 'Finished sampling haha!'\n",
       "\\end{enumerate*}\n"
      ],
      "text/markdown": [
       "1. ''\n",
       "2. '--- Translating Stan model to C++ code ---'\n",
       "3. <span style=white-space:pre-wrap>'bin/stanc  --o=../Hokkaido_Wuhan_IncubationPeriod_2020/scripts/Andrei-more_accurate/stan-sims/data_incper_inclwuhan-lognormal-no_truncation/fit.hpp ../Hokkaido_Wuhan_IncubationPeriod_2020/scripts/Andrei-more_accurate/stan-sims/data_incper_inclwuhan-lognormal-no_truncation/fit.stan'</span>\n",
       "4. <span style=white-space:pre-wrap>'mpicxx -fvisibility-inlines-hidden -std=c++17 -fmessage-length=0 -march=nocona -mtune=haswell -ftree-vectorize -fPIC -fstack-protector-strong -fno-plt -O2 -ffunction-sections -pipe -isystem /home/aakhmetz/anaconda3/include -std=c++1y -D_REENTRANT -Wno-sign-compare    -Wno-delete-non-virtual-dtor  -I stan/lib/stan_math/lib/tbb_2019_U8/include -O3 -I src -I stan/src -I lib/rapidjson_1.1.0/ -I stan/lib/stan_math/ -I stan/lib/stan_math/lib/eigen_3.3.3 -I stan/lib/stan_math/lib/boost_1.72.0 -I stan/lib/stan_math/lib/sundials_4.1.0/include -DNDEBUG -D_FORTIFY_SOURCE=2 -O2 -isystem /home/aakhmetz/anaconda3/include    -DBOOST_DISABLE_ASSERTS   -DSTAN_MPI   -c -MT ../Hokkaido_Wuhan_IncubationPeriod_2020/scripts/Andrei-more_accurate/stan-sims/data_incper_inclwuhan-lognormal-no_truncation/fit.o -MT ../Hokkaido_Wuhan_IncubationPeriod_2020/scripts/Andrei-more_accurate/stan-sims/data_incper_inclwuhan-lognormal-no_truncation/fit -include ../Hokkaido_Wuhan_IncubationPeriod_2020/scripts/Andrei-more_accurate/stan-sims/data_incper_inclwuhan-lognormal-no_truncation/fit.hpp -include src/cmdstan/main.cpp -MM -E -MG -MP -MF ../Hokkaido_Wuhan_IncubationPeriod_2020/scripts/Andrei-more_accurate/stan-sims/data_incper_inclwuhan-lognormal-no_truncation/fit.d ../Hokkaido_Wuhan_IncubationPeriod_2020/scripts/Andrei-more_accurate/stan-sims/data_incper_inclwuhan-lognormal-no_truncation/fit.hpp'</span>\n",
       "5. ''\n",
       "6. '--- Compiling, linking C++ code ---'\n",
       "7. <span style=white-space:pre-wrap>'mpicxx -fvisibility-inlines-hidden -std=c++17 -fmessage-length=0 -march=nocona -mtune=haswell -ftree-vectorize -fPIC -fstack-protector-strong -fno-plt -O2 -ffunction-sections -pipe -isystem /home/aakhmetz/anaconda3/include -std=c++1y -D_REENTRANT -Wno-sign-compare    -Wno-delete-non-virtual-dtor  -I stan/lib/stan_math/lib/tbb_2019_U8/include -O3 -I src -I stan/src -I lib/rapidjson_1.1.0/ -I stan/lib/stan_math/ -I stan/lib/stan_math/lib/eigen_3.3.3 -I stan/lib/stan_math/lib/boost_1.72.0 -I stan/lib/stan_math/lib/sundials_4.1.0/include -DNDEBUG -D_FORTIFY_SOURCE=2 -O2 -isystem /home/aakhmetz/anaconda3/include    -DBOOST_DISABLE_ASSERTS   -DSTAN_MPI   -c  -x c++ -o ../Hokkaido_Wuhan_IncubationPeriod_2020/scripts/Andrei-more_accurate/stan-sims/data_incper_inclwuhan-lognormal-no_truncation/fit.o ../Hokkaido_Wuhan_IncubationPeriod_2020/scripts/Andrei-more_accurate/stan-sims/data_incper_inclwuhan-lognormal-no_truncation/fit.hpp'</span>\n",
       "8. <span style=white-space:pre-wrap>'mpicxx -fvisibility-inlines-hidden -std=c++17 -fmessage-length=0 -march=nocona -mtune=haswell -ftree-vectorize -fPIC -fstack-protector-strong -fno-plt -O2 -ffunction-sections -pipe -isystem /home/aakhmetz/anaconda3/include -std=c++1y -D_REENTRANT -Wno-sign-compare    -Wno-delete-non-virtual-dtor  -I stan/lib/stan_math/lib/tbb_2019_U8/include -O3 -I src -I stan/src -I lib/rapidjson_1.1.0/ -I stan/lib/stan_math/ -I stan/lib/stan_math/lib/eigen_3.3.3 -I stan/lib/stan_math/lib/boost_1.72.0 -I stan/lib/stan_math/lib/sundials_4.1.0/include -DNDEBUG -D_FORTIFY_SOURCE=2 -O2 -isystem /home/aakhmetz/anaconda3/include    -DBOOST_DISABLE_ASSERTS   -DSTAN_MPI  -Wl,-O2 -Wl,--sort-common -Wl,--as-needed -Wl,-z,relro -Wl,-z,now -Wl,--disable-new-dtags -Wl,--gc-sections -Wl,-rpath,/home/aakhmetz/anaconda3/lib -Wl,-rpath-link,/home/aakhmetz/anaconda3/lib -L/home/aakhmetz/anaconda3/lib       -Wl,-L,\"/home/aakhmetz/Dropbox/CmdStan/stan/lib/stan_math/lib/boost_1.72.0/stage/lib\" -Wl,-rpath,\"/home/aakhmetz/Dropbox/CmdStan/stan/lib/stan_math/lib/boost_1.72.0/stage/lib\" -Wl,-L,\"/home/aakhmetz/Dropbox/CmdStan/stan/lib/stan_math/lib/tbb\" -Wl,-rpath,\"/home/aakhmetz/Dropbox/CmdStan/stan/lib/stan_math/lib/tbb\"  ../Hokkaido_Wuhan_IncubationPeriod_2020/scripts/Andrei-more_accurate/stan-sims/data_incper_inclwuhan-lognormal-no_truncation/fit.o src/cmdstan/main.o         stan/lib/stan_math/lib/sundials_4.1.0/lib/libsundials_nvecserial.a stan/lib/stan_math/lib/sundials_4.1.0/lib/libsundials_cvodes.a stan/lib/stan_math/lib/sundials_4.1.0/lib/libsundials_idas.a stan/lib/stan_math/lib/sundials_4.1.0/lib/libsundials_kinsol.a stan/lib/stan_math/lib/boost_1.72.0/stage/lib/libboost_serialization.so stan/lib/stan_math/lib/boost_1.72.0/stage/lib/libboost_mpi.so stan/lib/stan_math/stan/math/prim/functor/mpi_cluster_inst.o stan/lib/stan_math/lib/tbb/libtbb.so.2 -o ../Hokkaido_Wuhan_IncubationPeriod_2020/scripts/Andrei-more_accurate/stan-sims/data_incper_inclwuhan-lognormal-no_truncation/fit'</span>\n",
       "9. 'Running 1'\n",
       "10. 'Running 2'\n",
       "11. 'Running 3'\n",
       "12. 'Running 4'\n",
       "13. 'Finished sampling haha!'\n",
       "\n",
       "\n"
      ],
      "text/plain": [
       " [1] \"\"                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  \n",
       " [2] \"--- Translating Stan model to C++ code ---\"                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        \n",
       " [3] \"bin/stanc  --o=../Hokkaido_Wuhan_IncubationPeriod_2020/scripts/Andrei-more_accurate/stan-sims/data_incper_inclwuhan-lognormal-no_truncation/fit.hpp ../Hokkaido_Wuhan_IncubationPeriod_2020/scripts/Andrei-more_accurate/stan-sims/data_incper_inclwuhan-lognormal-no_truncation/fit.stan\"                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         \n",
       " [4] \"mpicxx -fvisibility-inlines-hidden -std=c++17 -fmessage-length=0 -march=nocona -mtune=haswell -ftree-vectorize -fPIC -fstack-protector-strong -fno-plt -O2 -ffunction-sections -pipe -isystem /home/aakhmetz/anaconda3/include -std=c++1y -D_REENTRANT -Wno-sign-compare    -Wno-delete-non-virtual-dtor  -I stan/lib/stan_math/lib/tbb_2019_U8/include -O3 -I src -I stan/src -I lib/rapidjson_1.1.0/ -I stan/lib/stan_math/ -I stan/lib/stan_math/lib/eigen_3.3.3 -I stan/lib/stan_math/lib/boost_1.72.0 -I stan/lib/stan_math/lib/sundials_4.1.0/include -DNDEBUG -D_FORTIFY_SOURCE=2 -O2 -isystem /home/aakhmetz/anaconda3/include    -DBOOST_DISABLE_ASSERTS   -DSTAN_MPI   -c -MT ../Hokkaido_Wuhan_IncubationPeriod_2020/scripts/Andrei-more_accurate/stan-sims/data_incper_inclwuhan-lognormal-no_truncation/fit.o -MT ../Hokkaido_Wuhan_IncubationPeriod_2020/scripts/Andrei-more_accurate/stan-sims/data_incper_inclwuhan-lognormal-no_truncation/fit -include ../Hokkaido_Wuhan_IncubationPeriod_2020/scripts/Andrei-more_accurate/stan-sims/data_incper_inclwuhan-lognormal-no_truncation/fit.hpp -include src/cmdstan/main.cpp -MM -E -MG -MP -MF ../Hokkaido_Wuhan_IncubationPeriod_2020/scripts/Andrei-more_accurate/stan-sims/data_incper_inclwuhan-lognormal-no_truncation/fit.d ../Hokkaido_Wuhan_IncubationPeriod_2020/scripts/Andrei-more_accurate/stan-sims/data_incper_inclwuhan-lognormal-no_truncation/fit.hpp\"                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            \n",
       " [5] \"\"                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  \n",
       " [6] \"--- Compiling, linking C++ code ---\"                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               \n",
       " [7] \"mpicxx -fvisibility-inlines-hidden -std=c++17 -fmessage-length=0 -march=nocona -mtune=haswell -ftree-vectorize -fPIC -fstack-protector-strong -fno-plt -O2 -ffunction-sections -pipe -isystem /home/aakhmetz/anaconda3/include -std=c++1y -D_REENTRANT -Wno-sign-compare    -Wno-delete-non-virtual-dtor  -I stan/lib/stan_math/lib/tbb_2019_U8/include -O3 -I src -I stan/src -I lib/rapidjson_1.1.0/ -I stan/lib/stan_math/ -I stan/lib/stan_math/lib/eigen_3.3.3 -I stan/lib/stan_math/lib/boost_1.72.0 -I stan/lib/stan_math/lib/sundials_4.1.0/include -DNDEBUG -D_FORTIFY_SOURCE=2 -O2 -isystem /home/aakhmetz/anaconda3/include    -DBOOST_DISABLE_ASSERTS   -DSTAN_MPI   -c  -x c++ -o ../Hokkaido_Wuhan_IncubationPeriod_2020/scripts/Andrei-more_accurate/stan-sims/data_incper_inclwuhan-lognormal-no_truncation/fit.o ../Hokkaido_Wuhan_IncubationPeriod_2020/scripts/Andrei-more_accurate/stan-sims/data_incper_inclwuhan-lognormal-no_truncation/fit.hpp\"                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            \n",
       " [8] \"mpicxx -fvisibility-inlines-hidden -std=c++17 -fmessage-length=0 -march=nocona -mtune=haswell -ftree-vectorize -fPIC -fstack-protector-strong -fno-plt -O2 -ffunction-sections -pipe -isystem /home/aakhmetz/anaconda3/include -std=c++1y -D_REENTRANT -Wno-sign-compare    -Wno-delete-non-virtual-dtor  -I stan/lib/stan_math/lib/tbb_2019_U8/include -O3 -I src -I stan/src -I lib/rapidjson_1.1.0/ -I stan/lib/stan_math/ -I stan/lib/stan_math/lib/eigen_3.3.3 -I stan/lib/stan_math/lib/boost_1.72.0 -I stan/lib/stan_math/lib/sundials_4.1.0/include -DNDEBUG -D_FORTIFY_SOURCE=2 -O2 -isystem /home/aakhmetz/anaconda3/include    -DBOOST_DISABLE_ASSERTS   -DSTAN_MPI  -Wl,-O2 -Wl,--sort-common -Wl,--as-needed -Wl,-z,relro -Wl,-z,now -Wl,--disable-new-dtags -Wl,--gc-sections -Wl,-rpath,/home/aakhmetz/anaconda3/lib -Wl,-rpath-link,/home/aakhmetz/anaconda3/lib -L/home/aakhmetz/anaconda3/lib       -Wl,-L,\\\"/home/aakhmetz/Dropbox/CmdStan/stan/lib/stan_math/lib/boost_1.72.0/stage/lib\\\" -Wl,-rpath,\\\"/home/aakhmetz/Dropbox/CmdStan/stan/lib/stan_math/lib/boost_1.72.0/stage/lib\\\" -Wl,-L,\\\"/home/aakhmetz/Dropbox/CmdStan/stan/lib/stan_math/lib/tbb\\\" -Wl,-rpath,\\\"/home/aakhmetz/Dropbox/CmdStan/stan/lib/stan_math/lib/tbb\\\"  ../Hokkaido_Wuhan_IncubationPeriod_2020/scripts/Andrei-more_accurate/stan-sims/data_incper_inclwuhan-lognormal-no_truncation/fit.o src/cmdstan/main.o         stan/lib/stan_math/lib/sundials_4.1.0/lib/libsundials_nvecserial.a stan/lib/stan_math/lib/sundials_4.1.0/lib/libsundials_cvodes.a stan/lib/stan_math/lib/sundials_4.1.0/lib/libsundials_idas.a stan/lib/stan_math/lib/sundials_4.1.0/lib/libsundials_kinsol.a stan/lib/stan_math/lib/boost_1.72.0/stage/lib/libboost_serialization.so stan/lib/stan_math/lib/boost_1.72.0/stage/lib/libboost_mpi.so stan/lib/stan_math/stan/math/prim/functor/mpi_cluster_inst.o stan/lib/stan_math/lib/tbb/libtbb.so.2 -o ../Hokkaido_Wuhan_IncubationPeriod_2020/scripts/Andrei-more_accurate/stan-sims/data_incper_inclwuhan-lognormal-no_truncation/fit\"\n",
       " [9] \"Running 1\"                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         \n",
       "[10] \"Running 2\"                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         \n",
       "[11] \"Running 3\"                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         \n",
       "[12] \"Running 4\"                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         \n",
       "[13] \"Finished sampling haha!\"                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           "
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "idx = length(filenames)\n",
    "{\n",
    "    ## main dir for Stan simulations\n",
    "    standirname = 'stan-sims/'%&%filenames[idx]%&%\"-lognormal-no_truncation\"\n",
    "    unlink(standirname, recursive=T)\n",
    "    dir.create(standirname)\n",
    "\n",
    "    datafilename = \"../../data/\"%&%filenames[idx]%&%\".csv\"\n",
    "    read.table(datafilename, sep=\",\", header=T) %>% select(EL,ER,SL,SR,tstar) %>% mutate(dist = (SR+SL)/2-(ER+EL)/2) -> df -> df\n",
    "    df %>% filter(EL>0) -> df1 # E_L is defined\n",
    "    df %>% filter(EL==0) -> df2 # E_L is missing\n",
    "    df = rbind(df1,df2) # we move all incomplete records to the end of the dataframe\n",
    "\n",
    "    # Dumping data\n",
    "    N = nrow(df)\n",
    "    N2 = nrow(df2)\n",
    "    N1 = N - N2\n",
    "    E_L = df$EL\n",
    "    E_R = df$ER\n",
    "    S_L = df$SL\n",
    "    S_R = df$SR\n",
    "    stan_rdump(c('E_L', 'E_R', 'S_L', 'S_R', 'N', 'N2'), file=standirname%&%\"/Data.R\") \n",
    "  \n",
    "    # Dumping initial conditions\n",
    "    logmean_SI = log(mean(df$dist))\n",
    "    logsd_SI = log(sd(df$dist))\n",
    "    s_raw = rep(.6, N)\n",
    "    e_raw = rep(.4, N1)\n",
    "    t = rep(5.0, N2)\n",
    "    E_L_est_raw = rep(.5, N2)\n",
    "    stan_rdump(c('t', 'E_L_est_raw', 's_raw', 'e_raw', 'logmean_SI', 'logsd_SI'), file=standirname%&%\"/Init.R\")   \n",
    "\n",
    "    # Stan program\n",
    "    \"data {\n",
    "        int<lower = 0> N; // number of records\n",
    "        int<lower = 0> N2; // number of incomplete records\n",
    "        vector<lower = 0>[N] E_L;\n",
    "        vector<lower = 0>[N] E_R;\n",
    "        vector<lower = 0>[N] S_L;\n",
    "        vector<lower = 0>[N] S_R;\n",
    "    }\n",
    "\n",
    "\n",
    "    transformed data {\n",
    "        int<lower = 0> N1 = N - N2;\n",
    "    }\n",
    "\n",
    "    parameters {\n",
    "        real logmean_SI;\n",
    "        real logsd_SI;\n",
    "\n",
    "        vector<lower = 0, upper = 1>[N] s_raw;\n",
    "        vector<lower = 0, upper = 1>[N1] e_raw;\n",
    "        vector<lower = 0>[N2] t;\n",
    "        vector<lower = 0, upper = 1>[N2] E_L_est_raw;\n",
    "    }\n",
    "\n",
    "    transformed parameters {\n",
    "        real<lower = 0> param2 = sqrt(log((exp(2*(logsd_SI-logmean_SI))+1.0)));\n",
    "        real param1 = logmean_SI - param2^2/2.0;\n",
    "\n",
    "        vector<lower = min(S_L), upper = max(S_R)>[N] s;\n",
    "        vector<lower = min(E_L), upper = max(S_R)>[N1] e;\n",
    "        vector<lower = 0, upper = max(E_R)>[N2] E_L_est;\n",
    "\n",
    "        s = S_L + (S_R - S_L) .* s_raw;\n",
    "        for (k in 1:N1) {\n",
    "            if (E_R[k] > s[k]) \n",
    "                e[k] = E_L[k] + (s[k] - E_L[k]) * e_raw[k];\n",
    "            else\n",
    "                e[k] = E_L[k] + (E_R[k] - E_L[k]) * e_raw[k];\n",
    "        }\n",
    "\n",
    "        for (m in 1:N2) \n",
    "            E_L_est[m] = E_R[N1 + m] * E_L_est_raw[m];\n",
    "    }\n",
    "\n",
    "    model {\n",
    "        logmean_SI ~ std_normal();\n",
    "        logsd_SI ~ std_normal();\n",
    "\n",
    "        e_raw ~ normal(0.5, 1.0);\n",
    "        s_raw ~ normal(0.5, 1.0);\n",
    "        t ~ normal(5.0, 10.0);\n",
    "        E_L_est_raw ~ normal(0.5, 1.0);\n",
    "\n",
    "        for (k in 1:N1) \n",
    "            target += lognormal_lpdf(s[k] - e[k] | param1, param2);\n",
    "\n",
    "        for (m in 1:N2) {\n",
    "            target += lognormal_lpdf(t[m] | param1, param2) + lognormal_lcdf(s[N1 + m] - E_L_est[m] | param1, param2);\n",
    "            if (s[N1 + m] > E_R[N1 + m])\n",
    "                target += lognormal_lccdf(s[N1 + m] - E_R[N1 + m] | param1, param2);       \n",
    "        }\n",
    "    }\n",
    "\n",
    "    generated quantities {\n",
    "        real<lower = 0> mean_SI = exp(param1 + param2^2/2);\n",
    "        real<lower = 0> sd_SI = sqrt((exp(param2^2)-1)*exp(2*param1+param2^2));\n",
    "\n",
    "        vector[N] log_likelihood;\n",
    "        for (k in 1:N1) \n",
    "            log_likelihood[k] = lognormal_lpdf(s[k] - e[k] | param1, param2);\n",
    "\n",
    "        for (m in 1:N2) {\n",
    "            log_likelihood[N1 + m] = lognormal_lpdf(t[m] | param1, param2) + lognormal_lcdf(s[N1 + m] - E_L_est[m] | param1, param2);\n",
    "            if (s[N1 + m] > E_R[N1 + m])\n",
    "                log_likelihood[N1 + m] += lognormal_lccdf(s[N1 + m] - E_R[N1 + m] | param1, param2);       \n",
    "        }\n",
    "    }\" %>% cat(file=standirname %&% \"/fit.stan\", sep=\"\", fill=TRUE)\n",
    "\n",
    "    standistribdir = \"../../../CmdStan\"\n",
    "    stanscriptdir = \"../Hokkaido_Wuhan_IncubationPeriod_2020/scripts/Andrei-more_accurate/\"%&%standirname\n",
    "    ## bash file\n",
    "    \"#!/bin/bash\n",
    "    cwd=$(pwd)\n",
    "    cd \"%&%standistribdir%&%\"\n",
    "    make -j6 \"%&%stanscriptdir%&%\"/fit\n",
    "    cd \"%&%stanscriptdir%&%\"\n",
    "    mkdir -p diagnostics\n",
    "    for i in {1..4}\n",
    "    do\n",
    "        echo Running ${i}\n",
    "        SEEDNUMBER=$((1+$i))\n",
    "        ./fit \\\\\n",
    "            method=sample num_samples=10000 num_warmup=10000 save_warmup=0 \\\\\n",
    "                adapt delta=0.98 \\\\\n",
    "                algorithm=hmc \\\\\n",
    "                    engine=nuts \\\\\n",
    "            random seed=${SEEDNUMBER} \\\\\n",
    "            id=$i \\\\\n",
    "            data file=Data.R \\\\\n",
    "            init=Init.R \\\\\n",
    "            output file=trace-$i.csv \\\\\n",
    "                diagnostic_file=diagnostics/diagnostics-$i.csv > diagnostics/output-$i.txt &\n",
    "    done\n",
    "    echo Finished sampling haha!\n",
    "    \" %>% cat(file=standirname%&%\"/fit.sh\", sep=\"\", fill=TRUE)\n",
    "\n",
    "    ## running the bash script\n",
    "    system(\"bash \"%&%standirname%&%\"/fit.sh\", intern = TRUE)\n",
    "}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## <font color=\"orange\">Gamma distribution</font>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<style>\n",
       ".list-inline {list-style: none; margin:0; padding: 0}\n",
       ".list-inline>li {display: inline-block}\n",
       ".list-inline>li:not(:last-child)::after {content: \"\\00b7\"; padding: 0 .5ex}\n",
       "</style>\n",
       "<ol class=list-inline><li>''</li><li>'--- Translating Stan model to C++ code ---'</li><li><span style=white-space:pre-wrap>'bin/stanc  --o=../Hokkaido_Wuhan_IncubationPeriod_2020/scripts/Andrei-more_accurate/stan-sims/data_incper_inclwuhan-gamma-no_truncation_good/fit.hpp ../Hokkaido_Wuhan_IncubationPeriod_2020/scripts/Andrei-more_accurate/stan-sims/data_incper_inclwuhan-gamma-no_truncation_good/fit.stan'</span></li><li><span style=white-space:pre-wrap>'mpicxx -fvisibility-inlines-hidden -std=c++17 -fmessage-length=0 -march=nocona -mtune=haswell -ftree-vectorize -fPIC -fstack-protector-strong -fno-plt -O2 -ffunction-sections -pipe -isystem /home/aakhmetz/anaconda3/include -std=c++1y -D_REENTRANT -Wno-sign-compare    -Wno-delete-non-virtual-dtor  -I stan/lib/stan_math/lib/tbb_2019_U8/include -O3 -I src -I stan/src -I lib/rapidjson_1.1.0/ -I stan/lib/stan_math/ -I stan/lib/stan_math/lib/eigen_3.3.3 -I stan/lib/stan_math/lib/boost_1.72.0 -I stan/lib/stan_math/lib/sundials_4.1.0/include -DNDEBUG -D_FORTIFY_SOURCE=2 -O2 -isystem /home/aakhmetz/anaconda3/include    -DBOOST_DISABLE_ASSERTS   -DSTAN_MPI   -c -MT ../Hokkaido_Wuhan_IncubationPeriod_2020/scripts/Andrei-more_accurate/stan-sims/data_incper_inclwuhan-gamma-no_truncation_good/fit.o -MT ../Hokkaido_Wuhan_IncubationPeriod_2020/scripts/Andrei-more_accurate/stan-sims/data_incper_inclwuhan-gamma-no_truncation_good/fit -include ../Hokkaido_Wuhan_IncubationPeriod_2020/scripts/Andrei-more_accurate/stan-sims/data_incper_inclwuhan-gamma-no_truncation_good/fit.hpp -include src/cmdstan/main.cpp -MM -E -MG -MP -MF ../Hokkaido_Wuhan_IncubationPeriod_2020/scripts/Andrei-more_accurate/stan-sims/data_incper_inclwuhan-gamma-no_truncation_good/fit.d ../Hokkaido_Wuhan_IncubationPeriod_2020/scripts/Andrei-more_accurate/stan-sims/data_incper_inclwuhan-gamma-no_truncation_good/fit.hpp'</span></li><li>''</li><li>'--- Compiling, linking C++ code ---'</li><li><span style=white-space:pre-wrap>'mpicxx -fvisibility-inlines-hidden -std=c++17 -fmessage-length=0 -march=nocona -mtune=haswell -ftree-vectorize -fPIC -fstack-protector-strong -fno-plt -O2 -ffunction-sections -pipe -isystem /home/aakhmetz/anaconda3/include -std=c++1y -D_REENTRANT -Wno-sign-compare    -Wno-delete-non-virtual-dtor  -I stan/lib/stan_math/lib/tbb_2019_U8/include -O3 -I src -I stan/src -I lib/rapidjson_1.1.0/ -I stan/lib/stan_math/ -I stan/lib/stan_math/lib/eigen_3.3.3 -I stan/lib/stan_math/lib/boost_1.72.0 -I stan/lib/stan_math/lib/sundials_4.1.0/include -DNDEBUG -D_FORTIFY_SOURCE=2 -O2 -isystem /home/aakhmetz/anaconda3/include    -DBOOST_DISABLE_ASSERTS   -DSTAN_MPI   -c  -x c++ -o ../Hokkaido_Wuhan_IncubationPeriod_2020/scripts/Andrei-more_accurate/stan-sims/data_incper_inclwuhan-gamma-no_truncation_good/fit.o ../Hokkaido_Wuhan_IncubationPeriod_2020/scripts/Andrei-more_accurate/stan-sims/data_incper_inclwuhan-gamma-no_truncation_good/fit.hpp'</span></li><li><span style=white-space:pre-wrap>'mpicxx -fvisibility-inlines-hidden -std=c++17 -fmessage-length=0 -march=nocona -mtune=haswell -ftree-vectorize -fPIC -fstack-protector-strong -fno-plt -O2 -ffunction-sections -pipe -isystem /home/aakhmetz/anaconda3/include -std=c++1y -D_REENTRANT -Wno-sign-compare    -Wno-delete-non-virtual-dtor  -I stan/lib/stan_math/lib/tbb_2019_U8/include -O3 -I src -I stan/src -I lib/rapidjson_1.1.0/ -I stan/lib/stan_math/ -I stan/lib/stan_math/lib/eigen_3.3.3 -I stan/lib/stan_math/lib/boost_1.72.0 -I stan/lib/stan_math/lib/sundials_4.1.0/include -DNDEBUG -D_FORTIFY_SOURCE=2 -O2 -isystem /home/aakhmetz/anaconda3/include    -DBOOST_DISABLE_ASSERTS   -DSTAN_MPI  -Wl,-O2 -Wl,--sort-common -Wl,--as-needed -Wl,-z,relro -Wl,-z,now -Wl,--disable-new-dtags -Wl,--gc-sections -Wl,-rpath,/home/aakhmetz/anaconda3/lib -Wl,-rpath-link,/home/aakhmetz/anaconda3/lib -L/home/aakhmetz/anaconda3/lib       -Wl,-L,\"/home/aakhmetz/Dropbox/CmdStan/stan/lib/stan_math/lib/boost_1.72.0/stage/lib\" -Wl,-rpath,\"/home/aakhmetz/Dropbox/CmdStan/stan/lib/stan_math/lib/boost_1.72.0/stage/lib\" -Wl,-L,\"/home/aakhmetz/Dropbox/CmdStan/stan/lib/stan_math/lib/tbb\" -Wl,-rpath,\"/home/aakhmetz/Dropbox/CmdStan/stan/lib/stan_math/lib/tbb\"  ../Hokkaido_Wuhan_IncubationPeriod_2020/scripts/Andrei-more_accurate/stan-sims/data_incper_inclwuhan-gamma-no_truncation_good/fit.o src/cmdstan/main.o         stan/lib/stan_math/lib/sundials_4.1.0/lib/libsundials_nvecserial.a stan/lib/stan_math/lib/sundials_4.1.0/lib/libsundials_cvodes.a stan/lib/stan_math/lib/sundials_4.1.0/lib/libsundials_idas.a stan/lib/stan_math/lib/sundials_4.1.0/lib/libsundials_kinsol.a stan/lib/stan_math/lib/boost_1.72.0/stage/lib/libboost_serialization.so stan/lib/stan_math/lib/boost_1.72.0/stage/lib/libboost_mpi.so stan/lib/stan_math/stan/math/prim/functor/mpi_cluster_inst.o stan/lib/stan_math/lib/tbb/libtbb.so.2 -o ../Hokkaido_Wuhan_IncubationPeriod_2020/scripts/Andrei-more_accurate/stan-sims/data_incper_inclwuhan-gamma-no_truncation_good/fit'</span></li><li>'Running 1'</li><li>'Running 2'</li><li>'Running 3'</li><li>'Running 4'</li><li>'Finished sampling haha!'</li></ol>\n"
      ],
      "text/latex": [
       "\\begin{enumerate*}\n",
       "\\item ''\n",
       "\\item '--- Translating Stan model to C++ code ---'\n",
       "\\item 'bin/stanc  --o=../Hokkaido\\_Wuhan\\_IncubationPeriod\\_2020/scripts/Andrei-more\\_accurate/stan-sims/data\\_incper\\_inclwuhan-gamma-no\\_truncation\\_good/fit.hpp ../Hokkaido\\_Wuhan\\_IncubationPeriod\\_2020/scripts/Andrei-more\\_accurate/stan-sims/data\\_incper\\_inclwuhan-gamma-no\\_truncation\\_good/fit.stan'\n",
       "\\item 'mpicxx -fvisibility-inlines-hidden -std=c++17 -fmessage-length=0 -march=nocona -mtune=haswell -ftree-vectorize -fPIC -fstack-protector-strong -fno-plt -O2 -ffunction-sections -pipe -isystem /home/aakhmetz/anaconda3/include -std=c++1y -D\\_REENTRANT -Wno-sign-compare    -Wno-delete-non-virtual-dtor  -I stan/lib/stan\\_math/lib/tbb\\_2019\\_U8/include -O3 -I src -I stan/src -I lib/rapidjson\\_1.1.0/ -I stan/lib/stan\\_math/ -I stan/lib/stan\\_math/lib/eigen\\_3.3.3 -I stan/lib/stan\\_math/lib/boost\\_1.72.0 -I stan/lib/stan\\_math/lib/sundials\\_4.1.0/include -DNDEBUG -D\\_FORTIFY\\_SOURCE=2 -O2 -isystem /home/aakhmetz/anaconda3/include    -DBOOST\\_DISABLE\\_ASSERTS   -DSTAN\\_MPI   -c -MT ../Hokkaido\\_Wuhan\\_IncubationPeriod\\_2020/scripts/Andrei-more\\_accurate/stan-sims/data\\_incper\\_inclwuhan-gamma-no\\_truncation\\_good/fit.o -MT ../Hokkaido\\_Wuhan\\_IncubationPeriod\\_2020/scripts/Andrei-more\\_accurate/stan-sims/data\\_incper\\_inclwuhan-gamma-no\\_truncation\\_good/fit -include ../Hokkaido\\_Wuhan\\_IncubationPeriod\\_2020/scripts/Andrei-more\\_accurate/stan-sims/data\\_incper\\_inclwuhan-gamma-no\\_truncation\\_good/fit.hpp -include src/cmdstan/main.cpp -MM -E -MG -MP -MF ../Hokkaido\\_Wuhan\\_IncubationPeriod\\_2020/scripts/Andrei-more\\_accurate/stan-sims/data\\_incper\\_inclwuhan-gamma-no\\_truncation\\_good/fit.d ../Hokkaido\\_Wuhan\\_IncubationPeriod\\_2020/scripts/Andrei-more\\_accurate/stan-sims/data\\_incper\\_inclwuhan-gamma-no\\_truncation\\_good/fit.hpp'\n",
       "\\item ''\n",
       "\\item '--- Compiling, linking C++ code ---'\n",
       "\\item 'mpicxx -fvisibility-inlines-hidden -std=c++17 -fmessage-length=0 -march=nocona -mtune=haswell -ftree-vectorize -fPIC -fstack-protector-strong -fno-plt -O2 -ffunction-sections -pipe -isystem /home/aakhmetz/anaconda3/include -std=c++1y -D\\_REENTRANT -Wno-sign-compare    -Wno-delete-non-virtual-dtor  -I stan/lib/stan\\_math/lib/tbb\\_2019\\_U8/include -O3 -I src -I stan/src -I lib/rapidjson\\_1.1.0/ -I stan/lib/stan\\_math/ -I stan/lib/stan\\_math/lib/eigen\\_3.3.3 -I stan/lib/stan\\_math/lib/boost\\_1.72.0 -I stan/lib/stan\\_math/lib/sundials\\_4.1.0/include -DNDEBUG -D\\_FORTIFY\\_SOURCE=2 -O2 -isystem /home/aakhmetz/anaconda3/include    -DBOOST\\_DISABLE\\_ASSERTS   -DSTAN\\_MPI   -c  -x c++ -o ../Hokkaido\\_Wuhan\\_IncubationPeriod\\_2020/scripts/Andrei-more\\_accurate/stan-sims/data\\_incper\\_inclwuhan-gamma-no\\_truncation\\_good/fit.o ../Hokkaido\\_Wuhan\\_IncubationPeriod\\_2020/scripts/Andrei-more\\_accurate/stan-sims/data\\_incper\\_inclwuhan-gamma-no\\_truncation\\_good/fit.hpp'\n",
       "\\item 'mpicxx -fvisibility-inlines-hidden -std=c++17 -fmessage-length=0 -march=nocona -mtune=haswell -ftree-vectorize -fPIC -fstack-protector-strong -fno-plt -O2 -ffunction-sections -pipe -isystem /home/aakhmetz/anaconda3/include -std=c++1y -D\\_REENTRANT -Wno-sign-compare    -Wno-delete-non-virtual-dtor  -I stan/lib/stan\\_math/lib/tbb\\_2019\\_U8/include -O3 -I src -I stan/src -I lib/rapidjson\\_1.1.0/ -I stan/lib/stan\\_math/ -I stan/lib/stan\\_math/lib/eigen\\_3.3.3 -I stan/lib/stan\\_math/lib/boost\\_1.72.0 -I stan/lib/stan\\_math/lib/sundials\\_4.1.0/include -DNDEBUG -D\\_FORTIFY\\_SOURCE=2 -O2 -isystem /home/aakhmetz/anaconda3/include    -DBOOST\\_DISABLE\\_ASSERTS   -DSTAN\\_MPI  -Wl,-O2 -Wl,--sort-common -Wl,--as-needed -Wl,-z,relro -Wl,-z,now -Wl,--disable-new-dtags -Wl,--gc-sections -Wl,-rpath,/home/aakhmetz/anaconda3/lib -Wl,-rpath-link,/home/aakhmetz/anaconda3/lib -L/home/aakhmetz/anaconda3/lib       -Wl,-L,\"/home/aakhmetz/Dropbox/CmdStan/stan/lib/stan\\_math/lib/boost\\_1.72.0/stage/lib\" -Wl,-rpath,\"/home/aakhmetz/Dropbox/CmdStan/stan/lib/stan\\_math/lib/boost\\_1.72.0/stage/lib\" -Wl,-L,\"/home/aakhmetz/Dropbox/CmdStan/stan/lib/stan\\_math/lib/tbb\" -Wl,-rpath,\"/home/aakhmetz/Dropbox/CmdStan/stan/lib/stan\\_math/lib/tbb\"  ../Hokkaido\\_Wuhan\\_IncubationPeriod\\_2020/scripts/Andrei-more\\_accurate/stan-sims/data\\_incper\\_inclwuhan-gamma-no\\_truncation\\_good/fit.o src/cmdstan/main.o         stan/lib/stan\\_math/lib/sundials\\_4.1.0/lib/libsundials\\_nvecserial.a stan/lib/stan\\_math/lib/sundials\\_4.1.0/lib/libsundials\\_cvodes.a stan/lib/stan\\_math/lib/sundials\\_4.1.0/lib/libsundials\\_idas.a stan/lib/stan\\_math/lib/sundials\\_4.1.0/lib/libsundials\\_kinsol.a stan/lib/stan\\_math/lib/boost\\_1.72.0/stage/lib/libboost\\_serialization.so stan/lib/stan\\_math/lib/boost\\_1.72.0/stage/lib/libboost\\_mpi.so stan/lib/stan\\_math/stan/math/prim/functor/mpi\\_cluster\\_inst.o stan/lib/stan\\_math/lib/tbb/libtbb.so.2 -o ../Hokkaido\\_Wuhan\\_IncubationPeriod\\_2020/scripts/Andrei-more\\_accurate/stan-sims/data\\_incper\\_inclwuhan-gamma-no\\_truncation\\_good/fit'\n",
       "\\item 'Running 1'\n",
       "\\item 'Running 2'\n",
       "\\item 'Running 3'\n",
       "\\item 'Running 4'\n",
       "\\item 'Finished sampling haha!'\n",
       "\\end{enumerate*}\n"
      ],
      "text/markdown": [
       "1. ''\n",
       "2. '--- Translating Stan model to C++ code ---'\n",
       "3. <span style=white-space:pre-wrap>'bin/stanc  --o=../Hokkaido_Wuhan_IncubationPeriod_2020/scripts/Andrei-more_accurate/stan-sims/data_incper_inclwuhan-gamma-no_truncation_good/fit.hpp ../Hokkaido_Wuhan_IncubationPeriod_2020/scripts/Andrei-more_accurate/stan-sims/data_incper_inclwuhan-gamma-no_truncation_good/fit.stan'</span>\n",
       "4. <span style=white-space:pre-wrap>'mpicxx -fvisibility-inlines-hidden -std=c++17 -fmessage-length=0 -march=nocona -mtune=haswell -ftree-vectorize -fPIC -fstack-protector-strong -fno-plt -O2 -ffunction-sections -pipe -isystem /home/aakhmetz/anaconda3/include -std=c++1y -D_REENTRANT -Wno-sign-compare    -Wno-delete-non-virtual-dtor  -I stan/lib/stan_math/lib/tbb_2019_U8/include -O3 -I src -I stan/src -I lib/rapidjson_1.1.0/ -I stan/lib/stan_math/ -I stan/lib/stan_math/lib/eigen_3.3.3 -I stan/lib/stan_math/lib/boost_1.72.0 -I stan/lib/stan_math/lib/sundials_4.1.0/include -DNDEBUG -D_FORTIFY_SOURCE=2 -O2 -isystem /home/aakhmetz/anaconda3/include    -DBOOST_DISABLE_ASSERTS   -DSTAN_MPI   -c -MT ../Hokkaido_Wuhan_IncubationPeriod_2020/scripts/Andrei-more_accurate/stan-sims/data_incper_inclwuhan-gamma-no_truncation_good/fit.o -MT ../Hokkaido_Wuhan_IncubationPeriod_2020/scripts/Andrei-more_accurate/stan-sims/data_incper_inclwuhan-gamma-no_truncation_good/fit -include ../Hokkaido_Wuhan_IncubationPeriod_2020/scripts/Andrei-more_accurate/stan-sims/data_incper_inclwuhan-gamma-no_truncation_good/fit.hpp -include src/cmdstan/main.cpp -MM -E -MG -MP -MF ../Hokkaido_Wuhan_IncubationPeriod_2020/scripts/Andrei-more_accurate/stan-sims/data_incper_inclwuhan-gamma-no_truncation_good/fit.d ../Hokkaido_Wuhan_IncubationPeriod_2020/scripts/Andrei-more_accurate/stan-sims/data_incper_inclwuhan-gamma-no_truncation_good/fit.hpp'</span>\n",
       "5. ''\n",
       "6. '--- Compiling, linking C++ code ---'\n",
       "7. <span style=white-space:pre-wrap>'mpicxx -fvisibility-inlines-hidden -std=c++17 -fmessage-length=0 -march=nocona -mtune=haswell -ftree-vectorize -fPIC -fstack-protector-strong -fno-plt -O2 -ffunction-sections -pipe -isystem /home/aakhmetz/anaconda3/include -std=c++1y -D_REENTRANT -Wno-sign-compare    -Wno-delete-non-virtual-dtor  -I stan/lib/stan_math/lib/tbb_2019_U8/include -O3 -I src -I stan/src -I lib/rapidjson_1.1.0/ -I stan/lib/stan_math/ -I stan/lib/stan_math/lib/eigen_3.3.3 -I stan/lib/stan_math/lib/boost_1.72.0 -I stan/lib/stan_math/lib/sundials_4.1.0/include -DNDEBUG -D_FORTIFY_SOURCE=2 -O2 -isystem /home/aakhmetz/anaconda3/include    -DBOOST_DISABLE_ASSERTS   -DSTAN_MPI   -c  -x c++ -o ../Hokkaido_Wuhan_IncubationPeriod_2020/scripts/Andrei-more_accurate/stan-sims/data_incper_inclwuhan-gamma-no_truncation_good/fit.o ../Hokkaido_Wuhan_IncubationPeriod_2020/scripts/Andrei-more_accurate/stan-sims/data_incper_inclwuhan-gamma-no_truncation_good/fit.hpp'</span>\n",
       "8. <span style=white-space:pre-wrap>'mpicxx -fvisibility-inlines-hidden -std=c++17 -fmessage-length=0 -march=nocona -mtune=haswell -ftree-vectorize -fPIC -fstack-protector-strong -fno-plt -O2 -ffunction-sections -pipe -isystem /home/aakhmetz/anaconda3/include -std=c++1y -D_REENTRANT -Wno-sign-compare    -Wno-delete-non-virtual-dtor  -I stan/lib/stan_math/lib/tbb_2019_U8/include -O3 -I src -I stan/src -I lib/rapidjson_1.1.0/ -I stan/lib/stan_math/ -I stan/lib/stan_math/lib/eigen_3.3.3 -I stan/lib/stan_math/lib/boost_1.72.0 -I stan/lib/stan_math/lib/sundials_4.1.0/include -DNDEBUG -D_FORTIFY_SOURCE=2 -O2 -isystem /home/aakhmetz/anaconda3/include    -DBOOST_DISABLE_ASSERTS   -DSTAN_MPI  -Wl,-O2 -Wl,--sort-common -Wl,--as-needed -Wl,-z,relro -Wl,-z,now -Wl,--disable-new-dtags -Wl,--gc-sections -Wl,-rpath,/home/aakhmetz/anaconda3/lib -Wl,-rpath-link,/home/aakhmetz/anaconda3/lib -L/home/aakhmetz/anaconda3/lib       -Wl,-L,\"/home/aakhmetz/Dropbox/CmdStan/stan/lib/stan_math/lib/boost_1.72.0/stage/lib\" -Wl,-rpath,\"/home/aakhmetz/Dropbox/CmdStan/stan/lib/stan_math/lib/boost_1.72.0/stage/lib\" -Wl,-L,\"/home/aakhmetz/Dropbox/CmdStan/stan/lib/stan_math/lib/tbb\" -Wl,-rpath,\"/home/aakhmetz/Dropbox/CmdStan/stan/lib/stan_math/lib/tbb\"  ../Hokkaido_Wuhan_IncubationPeriod_2020/scripts/Andrei-more_accurate/stan-sims/data_incper_inclwuhan-gamma-no_truncation_good/fit.o src/cmdstan/main.o         stan/lib/stan_math/lib/sundials_4.1.0/lib/libsundials_nvecserial.a stan/lib/stan_math/lib/sundials_4.1.0/lib/libsundials_cvodes.a stan/lib/stan_math/lib/sundials_4.1.0/lib/libsundials_idas.a stan/lib/stan_math/lib/sundials_4.1.0/lib/libsundials_kinsol.a stan/lib/stan_math/lib/boost_1.72.0/stage/lib/libboost_serialization.so stan/lib/stan_math/lib/boost_1.72.0/stage/lib/libboost_mpi.so stan/lib/stan_math/stan/math/prim/functor/mpi_cluster_inst.o stan/lib/stan_math/lib/tbb/libtbb.so.2 -o ../Hokkaido_Wuhan_IncubationPeriod_2020/scripts/Andrei-more_accurate/stan-sims/data_incper_inclwuhan-gamma-no_truncation_good/fit'</span>\n",
       "9. 'Running 1'\n",
       "10. 'Running 2'\n",
       "11. 'Running 3'\n",
       "12. 'Running 4'\n",
       "13. 'Finished sampling haha!'\n",
       "\n",
       "\n"
      ],
      "text/plain": [
       " [1] \"\"                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    \n",
       " [2] \"--- Translating Stan model to C++ code ---\"                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          \n",
       " [3] \"bin/stanc  --o=../Hokkaido_Wuhan_IncubationPeriod_2020/scripts/Andrei-more_accurate/stan-sims/data_incper_inclwuhan-gamma-no_truncation_good/fit.hpp ../Hokkaido_Wuhan_IncubationPeriod_2020/scripts/Andrei-more_accurate/stan-sims/data_incper_inclwuhan-gamma-no_truncation_good/fit.stan\"                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         \n",
       " [4] \"mpicxx -fvisibility-inlines-hidden -std=c++17 -fmessage-length=0 -march=nocona -mtune=haswell -ftree-vectorize -fPIC -fstack-protector-strong -fno-plt -O2 -ffunction-sections -pipe -isystem /home/aakhmetz/anaconda3/include -std=c++1y -D_REENTRANT -Wno-sign-compare    -Wno-delete-non-virtual-dtor  -I stan/lib/stan_math/lib/tbb_2019_U8/include -O3 -I src -I stan/src -I lib/rapidjson_1.1.0/ -I stan/lib/stan_math/ -I stan/lib/stan_math/lib/eigen_3.3.3 -I stan/lib/stan_math/lib/boost_1.72.0 -I stan/lib/stan_math/lib/sundials_4.1.0/include -DNDEBUG -D_FORTIFY_SOURCE=2 -O2 -isystem /home/aakhmetz/anaconda3/include    -DBOOST_DISABLE_ASSERTS   -DSTAN_MPI   -c -MT ../Hokkaido_Wuhan_IncubationPeriod_2020/scripts/Andrei-more_accurate/stan-sims/data_incper_inclwuhan-gamma-no_truncation_good/fit.o -MT ../Hokkaido_Wuhan_IncubationPeriod_2020/scripts/Andrei-more_accurate/stan-sims/data_incper_inclwuhan-gamma-no_truncation_good/fit -include ../Hokkaido_Wuhan_IncubationPeriod_2020/scripts/Andrei-more_accurate/stan-sims/data_incper_inclwuhan-gamma-no_truncation_good/fit.hpp -include src/cmdstan/main.cpp -MM -E -MG -MP -MF ../Hokkaido_Wuhan_IncubationPeriod_2020/scripts/Andrei-more_accurate/stan-sims/data_incper_inclwuhan-gamma-no_truncation_good/fit.d ../Hokkaido_Wuhan_IncubationPeriod_2020/scripts/Andrei-more_accurate/stan-sims/data_incper_inclwuhan-gamma-no_truncation_good/fit.hpp\"                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         \n",
       " [5] \"\"                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    \n",
       " [6] \"--- Compiling, linking C++ code ---\"                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 \n",
       " [7] \"mpicxx -fvisibility-inlines-hidden -std=c++17 -fmessage-length=0 -march=nocona -mtune=haswell -ftree-vectorize -fPIC -fstack-protector-strong -fno-plt -O2 -ffunction-sections -pipe -isystem /home/aakhmetz/anaconda3/include -std=c++1y -D_REENTRANT -Wno-sign-compare    -Wno-delete-non-virtual-dtor  -I stan/lib/stan_math/lib/tbb_2019_U8/include -O3 -I src -I stan/src -I lib/rapidjson_1.1.0/ -I stan/lib/stan_math/ -I stan/lib/stan_math/lib/eigen_3.3.3 -I stan/lib/stan_math/lib/boost_1.72.0 -I stan/lib/stan_math/lib/sundials_4.1.0/include -DNDEBUG -D_FORTIFY_SOURCE=2 -O2 -isystem /home/aakhmetz/anaconda3/include    -DBOOST_DISABLE_ASSERTS   -DSTAN_MPI   -c  -x c++ -o ../Hokkaido_Wuhan_IncubationPeriod_2020/scripts/Andrei-more_accurate/stan-sims/data_incper_inclwuhan-gamma-no_truncation_good/fit.o ../Hokkaido_Wuhan_IncubationPeriod_2020/scripts/Andrei-more_accurate/stan-sims/data_incper_inclwuhan-gamma-no_truncation_good/fit.hpp\"                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            \n",
       " [8] \"mpicxx -fvisibility-inlines-hidden -std=c++17 -fmessage-length=0 -march=nocona -mtune=haswell -ftree-vectorize -fPIC -fstack-protector-strong -fno-plt -O2 -ffunction-sections -pipe -isystem /home/aakhmetz/anaconda3/include -std=c++1y -D_REENTRANT -Wno-sign-compare    -Wno-delete-non-virtual-dtor  -I stan/lib/stan_math/lib/tbb_2019_U8/include -O3 -I src -I stan/src -I lib/rapidjson_1.1.0/ -I stan/lib/stan_math/ -I stan/lib/stan_math/lib/eigen_3.3.3 -I stan/lib/stan_math/lib/boost_1.72.0 -I stan/lib/stan_math/lib/sundials_4.1.0/include -DNDEBUG -D_FORTIFY_SOURCE=2 -O2 -isystem /home/aakhmetz/anaconda3/include    -DBOOST_DISABLE_ASSERTS   -DSTAN_MPI  -Wl,-O2 -Wl,--sort-common -Wl,--as-needed -Wl,-z,relro -Wl,-z,now -Wl,--disable-new-dtags -Wl,--gc-sections -Wl,-rpath,/home/aakhmetz/anaconda3/lib -Wl,-rpath-link,/home/aakhmetz/anaconda3/lib -L/home/aakhmetz/anaconda3/lib       -Wl,-L,\\\"/home/aakhmetz/Dropbox/CmdStan/stan/lib/stan_math/lib/boost_1.72.0/stage/lib\\\" -Wl,-rpath,\\\"/home/aakhmetz/Dropbox/CmdStan/stan/lib/stan_math/lib/boost_1.72.0/stage/lib\\\" -Wl,-L,\\\"/home/aakhmetz/Dropbox/CmdStan/stan/lib/stan_math/lib/tbb\\\" -Wl,-rpath,\\\"/home/aakhmetz/Dropbox/CmdStan/stan/lib/stan_math/lib/tbb\\\"  ../Hokkaido_Wuhan_IncubationPeriod_2020/scripts/Andrei-more_accurate/stan-sims/data_incper_inclwuhan-gamma-no_truncation_good/fit.o src/cmdstan/main.o         stan/lib/stan_math/lib/sundials_4.1.0/lib/libsundials_nvecserial.a stan/lib/stan_math/lib/sundials_4.1.0/lib/libsundials_cvodes.a stan/lib/stan_math/lib/sundials_4.1.0/lib/libsundials_idas.a stan/lib/stan_math/lib/sundials_4.1.0/lib/libsundials_kinsol.a stan/lib/stan_math/lib/boost_1.72.0/stage/lib/libboost_serialization.so stan/lib/stan_math/lib/boost_1.72.0/stage/lib/libboost_mpi.so stan/lib/stan_math/stan/math/prim/functor/mpi_cluster_inst.o stan/lib/stan_math/lib/tbb/libtbb.so.2 -o ../Hokkaido_Wuhan_IncubationPeriod_2020/scripts/Andrei-more_accurate/stan-sims/data_incper_inclwuhan-gamma-no_truncation_good/fit\"\n",
       " [9] \"Running 1\"                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           \n",
       "[10] \"Running 2\"                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           \n",
       "[11] \"Running 3\"                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           \n",
       "[12] \"Running 4\"                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           \n",
       "[13] \"Finished sampling haha!\"                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             "
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "idx = length(filenames)\n",
    "{\n",
    "    ## main dir for Stan simulations\n",
    "    standirname = 'stan-sims/'%&%filenames[idx]%&%\"-gamma-no_truncation\"\n",
    "    unlink(standirname, recursive=T)\n",
    "    dir.create(standirname)\n",
    "\n",
    "    datafilename = \"../../data/\"%&%filenames[idx]%&%\".csv\"\n",
    "    read.table(datafilename, sep=\",\", header=T) %>% select(EL,ER,SL,SR,tstar) %>% mutate(dist = (SR+SL)/2-(ER+EL)/2) -> df -> df\n",
    "    df %>% filter(EL>0) -> df1 # E_L is defined\n",
    "    df %>% filter(EL==0) -> df2 # E_L is missing\n",
    "    df = rbind(df1,df2) # we move all incomplete records to the end of the dataframe\n",
    "\n",
    "    # Dumping data\n",
    "    N = nrow(df)\n",
    "    N2 = nrow(df2)\n",
    "    N1 = N - N2\n",
    "    E_L = df$EL\n",
    "    E_R = df$ER\n",
    "    S_L = df$SL\n",
    "    S_R = df$SR\n",
    "    stan_rdump(c('E_L', 'E_R', 'S_L', 'S_R', 'N', 'N2'), file=standirname%&%\"/Data.R\") \n",
    "  \n",
    "    # Dumping initial conditions\n",
    "    mean_SI = mean(df$dist)\n",
    "    sd_SI = sd(df$dist)\n",
    "    s_raw = rep(.6, N)\n",
    "    e_raw = rep(.4, N1)\n",
    "    t = rep(5.0, N2)\n",
    "    E_L_est_raw = rep(.5, N2)\n",
    "    stan_rdump(c('t', 'E_L_est_raw', 's_raw', 'e_raw', 'mean_SI', 'sd_SI'), file=standirname%&%\"/Init.R\")   \n",
    "\n",
    "    # Stan program\n",
    "    \"data {\n",
    "        int<lower = 0> N; // number of records\n",
    "        int<lower = 0> N2; // number of incomplete records\n",
    "        vector<lower = 0>[N] E_L;\n",
    "        vector<lower = 0>[N] E_R;\n",
    "        vector<lower = 0>[N] S_L;\n",
    "        vector<lower = 0>[N] S_R;\n",
    "    }\n",
    "\n",
    "    transformed data {\n",
    "        int<lower = 0> N1 = N - N2;\n",
    "    }\n",
    "\n",
    "    parameters {\n",
    "        real<lower=0> mean_SI;\n",
    "        real<lower=0> sd_SI;\n",
    "\n",
    "        vector<lower = 0, upper = 1>[N] s_raw;\n",
    "        vector<lower = 0, upper = 1>[N1] e_raw;\n",
    "        vector<lower = 0>[N2] t;\n",
    "        vector<lower = 0, upper = 1>[N2] E_L_est_raw;\n",
    "    }\n",
    "\n",
    "    transformed parameters {\n",
    "        real<lower = 0> param1 = (mean_SI/sd_SI)^2;\n",
    "        real<lower = 0> param2 = mean_SI/(sd_SI^2);\n",
    "\n",
    "        vector<lower = min(S_L), upper = max(S_R)>[N] s;\n",
    "        vector<lower = min(E_L), upper = max(S_R)>[N1] e;\n",
    "        vector<lower = 0, upper = max(E_R)>[N2] E_L_est;\n",
    "\n",
    "        s = S_L + (S_R - S_L) .* s_raw;\n",
    "        for (k in 1:N1) {\n",
    "            if (E_R[k] > s[k]) \n",
    "                e[k] = E_L[k] + (s[k] - E_L[k]) * e_raw[k];\n",
    "            else\n",
    "                e[k] = E_L[k] + (E_R[k] - E_L[k]) * e_raw[k];\n",
    "        }\n",
    "\n",
    "        for (m in 1:N2) \n",
    "            E_L_est[m] = E_R[N1 + m] * E_L_est_raw[m];\n",
    "    }\n",
    "\n",
    "    model {\n",
    "        mean_SI ~ normal(5.0, 10.0);\n",
    "        sd_SI ~ cauchy(0, 5.0);\n",
    "\n",
    "        e_raw ~ normal(0.5, 1.0);\n",
    "        s_raw ~ normal(0.5, 1.0);\n",
    "        t ~ normal(5.0, 10.0);\n",
    "        E_L_est_raw ~ normal(0.5, 1.0);\n",
    "\n",
    "        for (k in 1:N1) \n",
    "            target += gamma_lpdf(s[k] - e[k] | param1, param2);\n",
    "\n",
    "        for (m in 1:N2) {\n",
    "            target += gamma_lpdf(t[m] | param1, param2) + gamma_lcdf(s[N1 + m] - E_L_est[m] | param1, param2);\n",
    "            if (s[N1 + m] > E_R[N1 + m])\n",
    "                target += gamma_lccdf(s[N1 + m] - E_R[N1 + m] | param1, param2);       \n",
    "        }\n",
    "    }\n",
    "\n",
    "    generated quantities {\n",
    "        vector[N] log_likelihood;\n",
    "        for (k in 1:N1) \n",
    "            log_likelihood[k] = gamma_lpdf(s[k] - e[k] | param1, param2);\n",
    "\n",
    "        for (m in 1:N2) {\n",
    "            log_likelihood[N1 + m] = gamma_lpdf(t[m] | param1, param2) + gamma_lcdf(s[N1 + m] - E_L_est[m] | param1, param2);\n",
    "            if (s[N1 + m] > E_R[N1 + m])\n",
    "                log_likelihood[N1 + m] += gamma_lccdf(s[N1 + m] - E_R[N1 + m] | param1, param2);       \n",
    "        }\n",
    "    }\" %>% cat(file=standirname %&% \"/fit.stan\", sep=\"\", fill=TRUE)\n",
    "\n",
    "    standistribdir = \"../../../CmdStan\"\n",
    "    stanscriptdir = \"../Hokkaido_Wuhan_IncubationPeriod_2020/scripts/Andrei-more_accurate/\"%&%standirname\n",
    "    ## bash file\n",
    "    \"#!/bin/bash\n",
    "    cwd=$(pwd)\n",
    "    cd \"%&%standistribdir%&%\"\n",
    "    make -j6 \"%&%stanscriptdir%&%\"/fit\n",
    "    cd \"%&%stanscriptdir%&%\"\n",
    "    mkdir -p diagnostics\n",
    "    for i in {1..4}\n",
    "    do\n",
    "        echo Running ${i}\n",
    "        SEEDNUMBER=$((1+$i))\n",
    "        ./fit \\\\\n",
    "            method=sample num_samples=10000 num_warmup=10000 save_warmup=0 \\\\\n",
    "                adapt delta=0.98 \\\\\n",
    "                algorithm=hmc \\\\\n",
    "                    engine=nuts \\\\\n",
    "            random seed=${SEEDNUMBER} \\\\\n",
    "            id=$i \\\\\n",
    "            data file=Data.R \\\\\n",
    "            init=Init.R \\\\\n",
    "            output file=trace-$i.csv \\\\\n",
    "                diagnostic_file=diagnostics/diagnostics-$i.csv > diagnostics/output-$i.txt &\n",
    "    done\n",
    "    echo Finished sampling haha!\n",
    "    \" %>% cat(file=standirname%&%\"/fit.sh\", sep=\"\", fill=TRUE)\n",
    "\n",
    "    ## running the bash script\n",
    "    system(\"bash \"%&%standirname%&%\"/fit.sh\", intern = TRUE)\n",
    "}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## <font color=\"orange\">Weibull distribution</font>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<style>\n",
       ".list-inline {list-style: none; margin:0; padding: 0}\n",
       ".list-inline>li {display: inline-block}\n",
       ".list-inline>li:not(:last-child)::after {content: \"\\00b7\"; padding: 0 .5ex}\n",
       "</style>\n",
       "<ol class=list-inline><li>''</li><li>'--- Translating Stan model to C++ code ---'</li><li><span style=white-space:pre-wrap>'bin/stanc  --o=../Dropbox/Hokkaido_Wuhan_IncubationPeriod_2020/scripts/Andrei-more_accurate/stan-sims/data_incper_inclwuhan-weibull-no_truncation/fit.hpp ../Dropbox/Hokkaido_Wuhan_IncubationPeriod_2020/scripts/Andrei-more_accurate/stan-sims/data_incper_inclwuhan-weibull-no_truncation/fit.stan'</span></li><li><span style=white-space:pre-wrap>'mpicxx -fvisibility-inlines-hidden -std=c++17 -fmessage-length=0 -march=nocona -mtune=haswell -ftree-vectorize -fPIC -fstack-protector-strong -fno-plt -O2 -ffunction-sections -pipe -isystem /home/aakhmetz/anaconda3/include -std=c++1y -D_REENTRANT -Wno-sign-compare    -Wno-delete-non-virtual-dtor  -I stan/lib/stan_math/lib/tbb_2019_U8/include -O3 -I src -I stan/src -I lib/rapidjson_1.1.0/ -I stan/lib/stan_math/ -I stan/lib/stan_math/lib/eigen_3.3.3 -I stan/lib/stan_math/lib/boost_1.72.0 -I stan/lib/stan_math/lib/sundials_4.1.0/include -DNDEBUG -D_FORTIFY_SOURCE=2 -O2 -isystem /home/aakhmetz/anaconda3/include    -DBOOST_DISABLE_ASSERTS   -DSTAN_MPI   -c -MT ../Dropbox/Hokkaido_Wuhan_IncubationPeriod_2020/scripts/Andrei-more_accurate/stan-sims/data_incper_inclwuhan-weibull-no_truncation/fit.o -MT ../Dropbox/Hokkaido_Wuhan_IncubationPeriod_2020/scripts/Andrei-more_accurate/stan-sims/data_incper_inclwuhan-weibull-no_truncation/fit -include ../Dropbox/Hokkaido_Wuhan_IncubationPeriod_2020/scripts/Andrei-more_accurate/stan-sims/data_incper_inclwuhan-weibull-no_truncation/fit.hpp -include src/cmdstan/main.cpp -MM -E -MG -MP -MF ../Dropbox/Hokkaido_Wuhan_IncubationPeriod_2020/scripts/Andrei-more_accurate/stan-sims/data_incper_inclwuhan-weibull-no_truncation/fit.d ../Dropbox/Hokkaido_Wuhan_IncubationPeriod_2020/scripts/Andrei-more_accurate/stan-sims/data_incper_inclwuhan-weibull-no_truncation/fit.hpp'</span></li><li>''</li><li>'--- Compiling, linking C++ code ---'</li><li><span style=white-space:pre-wrap>'mpicxx -fvisibility-inlines-hidden -std=c++17 -fmessage-length=0 -march=nocona -mtune=haswell -ftree-vectorize -fPIC -fstack-protector-strong -fno-plt -O2 -ffunction-sections -pipe -isystem /home/aakhmetz/anaconda3/include -std=c++1y -D_REENTRANT -Wno-sign-compare    -Wno-delete-non-virtual-dtor  -I stan/lib/stan_math/lib/tbb_2019_U8/include -O3 -I src -I stan/src -I lib/rapidjson_1.1.0/ -I stan/lib/stan_math/ -I stan/lib/stan_math/lib/eigen_3.3.3 -I stan/lib/stan_math/lib/boost_1.72.0 -I stan/lib/stan_math/lib/sundials_4.1.0/include -DNDEBUG -D_FORTIFY_SOURCE=2 -O2 -isystem /home/aakhmetz/anaconda3/include    -DBOOST_DISABLE_ASSERTS   -DSTAN_MPI   -c  -x c++ -o ../Dropbox/Hokkaido_Wuhan_IncubationPeriod_2020/scripts/Andrei-more_accurate/stan-sims/data_incper_inclwuhan-weibull-no_truncation/fit.o ../Dropbox/Hokkaido_Wuhan_IncubationPeriod_2020/scripts/Andrei-more_accurate/stan-sims/data_incper_inclwuhan-weibull-no_truncation/fit.hpp'</span></li><li><span style=white-space:pre-wrap>'mpicxx -fvisibility-inlines-hidden -std=c++17 -fmessage-length=0 -march=nocona -mtune=haswell -ftree-vectorize -fPIC -fstack-protector-strong -fno-plt -O2 -ffunction-sections -pipe -isystem /home/aakhmetz/anaconda3/include -std=c++1y -D_REENTRANT -Wno-sign-compare    -Wno-delete-non-virtual-dtor  -I stan/lib/stan_math/lib/tbb_2019_U8/include -O3 -I src -I stan/src -I lib/rapidjson_1.1.0/ -I stan/lib/stan_math/ -I stan/lib/stan_math/lib/eigen_3.3.3 -I stan/lib/stan_math/lib/boost_1.72.0 -I stan/lib/stan_math/lib/sundials_4.1.0/include -DNDEBUG -D_FORTIFY_SOURCE=2 -O2 -isystem /home/aakhmetz/anaconda3/include    -DBOOST_DISABLE_ASSERTS   -DSTAN_MPI  -Wl,-O2 -Wl,--sort-common -Wl,--as-needed -Wl,-z,relro -Wl,-z,now -Wl,--disable-new-dtags -Wl,--gc-sections -Wl,-rpath,/home/aakhmetz/anaconda3/lib -Wl,-rpath-link,/home/aakhmetz/anaconda3/lib -L/home/aakhmetz/anaconda3/lib       -Wl,-L,\"/home/aakhmetz/CmdStan/stan/lib/stan_math/lib/boost_1.72.0/stage/lib\" -Wl,-rpath,\"/home/aakhmetz/CmdStan/stan/lib/stan_math/lib/boost_1.72.0/stage/lib\" -Wl,-L,\"/home/aakhmetz/CmdStan/stan/lib/stan_math/lib/tbb\" -Wl,-rpath,\"/home/aakhmetz/CmdStan/stan/lib/stan_math/lib/tbb\"  ../Dropbox/Hokkaido_Wuhan_IncubationPeriod_2020/scripts/Andrei-more_accurate/stan-sims/data_incper_inclwuhan-weibull-no_truncation/fit.o src/cmdstan/main.o         stan/lib/stan_math/lib/sundials_4.1.0/lib/libsundials_nvecserial.a stan/lib/stan_math/lib/sundials_4.1.0/lib/libsundials_cvodes.a stan/lib/stan_math/lib/sundials_4.1.0/lib/libsundials_idas.a stan/lib/stan_math/lib/sundials_4.1.0/lib/libsundials_kinsol.a stan/lib/stan_math/lib/boost_1.72.0/stage/lib/libboost_serialization.so stan/lib/stan_math/lib/boost_1.72.0/stage/lib/libboost_mpi.so stan/lib/stan_math/stan/math/prim/functor/mpi_cluster_inst.o stan/lib/stan_math/lib/tbb/libtbb.so.2 -o ../Dropbox/Hokkaido_Wuhan_IncubationPeriod_2020/scripts/Andrei-more_accurate/stan-sims/data_incper_inclwuhan-weibull-no_truncation/fit'</span></li><li>'Running 1'</li><li>'Running 2'</li><li>'Running 3'</li><li>'Running 4'</li><li>'Finished sampling haha!'</li></ol>\n"
      ],
      "text/latex": [
       "\\begin{enumerate*}\n",
       "\\item ''\n",
       "\\item '--- Translating Stan model to C++ code ---'\n",
       "\\item 'bin/stanc  --o=../Dropbox/Hokkaido\\_Wuhan\\_IncubationPeriod\\_2020/scripts/Andrei-more\\_accurate/stan-sims/data\\_incper\\_inclwuhan-weibull-no\\_truncation/fit.hpp ../Dropbox/Hokkaido\\_Wuhan\\_IncubationPeriod\\_2020/scripts/Andrei-more\\_accurate/stan-sims/data\\_incper\\_inclwuhan-weibull-no\\_truncation/fit.stan'\n",
       "\\item 'mpicxx -fvisibility-inlines-hidden -std=c++17 -fmessage-length=0 -march=nocona -mtune=haswell -ftree-vectorize -fPIC -fstack-protector-strong -fno-plt -O2 -ffunction-sections -pipe -isystem /home/aakhmetz/anaconda3/include -std=c++1y -D\\_REENTRANT -Wno-sign-compare    -Wno-delete-non-virtual-dtor  -I stan/lib/stan\\_math/lib/tbb\\_2019\\_U8/include -O3 -I src -I stan/src -I lib/rapidjson\\_1.1.0/ -I stan/lib/stan\\_math/ -I stan/lib/stan\\_math/lib/eigen\\_3.3.3 -I stan/lib/stan\\_math/lib/boost\\_1.72.0 -I stan/lib/stan\\_math/lib/sundials\\_4.1.0/include -DNDEBUG -D\\_FORTIFY\\_SOURCE=2 -O2 -isystem /home/aakhmetz/anaconda3/include    -DBOOST\\_DISABLE\\_ASSERTS   -DSTAN\\_MPI   -c -MT ../Dropbox/Hokkaido\\_Wuhan\\_IncubationPeriod\\_2020/scripts/Andrei-more\\_accurate/stan-sims/data\\_incper\\_inclwuhan-weibull-no\\_truncation/fit.o -MT ../Dropbox/Hokkaido\\_Wuhan\\_IncubationPeriod\\_2020/scripts/Andrei-more\\_accurate/stan-sims/data\\_incper\\_inclwuhan-weibull-no\\_truncation/fit -include ../Dropbox/Hokkaido\\_Wuhan\\_IncubationPeriod\\_2020/scripts/Andrei-more\\_accurate/stan-sims/data\\_incper\\_inclwuhan-weibull-no\\_truncation/fit.hpp -include src/cmdstan/main.cpp -MM -E -MG -MP -MF ../Dropbox/Hokkaido\\_Wuhan\\_IncubationPeriod\\_2020/scripts/Andrei-more\\_accurate/stan-sims/data\\_incper\\_inclwuhan-weibull-no\\_truncation/fit.d ../Dropbox/Hokkaido\\_Wuhan\\_IncubationPeriod\\_2020/scripts/Andrei-more\\_accurate/stan-sims/data\\_incper\\_inclwuhan-weibull-no\\_truncation/fit.hpp'\n",
       "\\item ''\n",
       "\\item '--- Compiling, linking C++ code ---'\n",
       "\\item 'mpicxx -fvisibility-inlines-hidden -std=c++17 -fmessage-length=0 -march=nocona -mtune=haswell -ftree-vectorize -fPIC -fstack-protector-strong -fno-plt -O2 -ffunction-sections -pipe -isystem /home/aakhmetz/anaconda3/include -std=c++1y -D\\_REENTRANT -Wno-sign-compare    -Wno-delete-non-virtual-dtor  -I stan/lib/stan\\_math/lib/tbb\\_2019\\_U8/include -O3 -I src -I stan/src -I lib/rapidjson\\_1.1.0/ -I stan/lib/stan\\_math/ -I stan/lib/stan\\_math/lib/eigen\\_3.3.3 -I stan/lib/stan\\_math/lib/boost\\_1.72.0 -I stan/lib/stan\\_math/lib/sundials\\_4.1.0/include -DNDEBUG -D\\_FORTIFY\\_SOURCE=2 -O2 -isystem /home/aakhmetz/anaconda3/include    -DBOOST\\_DISABLE\\_ASSERTS   -DSTAN\\_MPI   -c  -x c++ -o ../Dropbox/Hokkaido\\_Wuhan\\_IncubationPeriod\\_2020/scripts/Andrei-more\\_accurate/stan-sims/data\\_incper\\_inclwuhan-weibull-no\\_truncation/fit.o ../Dropbox/Hokkaido\\_Wuhan\\_IncubationPeriod\\_2020/scripts/Andrei-more\\_accurate/stan-sims/data\\_incper\\_inclwuhan-weibull-no\\_truncation/fit.hpp'\n",
       "\\item 'mpicxx -fvisibility-inlines-hidden -std=c++17 -fmessage-length=0 -march=nocona -mtune=haswell -ftree-vectorize -fPIC -fstack-protector-strong -fno-plt -O2 -ffunction-sections -pipe -isystem /home/aakhmetz/anaconda3/include -std=c++1y -D\\_REENTRANT -Wno-sign-compare    -Wno-delete-non-virtual-dtor  -I stan/lib/stan\\_math/lib/tbb\\_2019\\_U8/include -O3 -I src -I stan/src -I lib/rapidjson\\_1.1.0/ -I stan/lib/stan\\_math/ -I stan/lib/stan\\_math/lib/eigen\\_3.3.3 -I stan/lib/stan\\_math/lib/boost\\_1.72.0 -I stan/lib/stan\\_math/lib/sundials\\_4.1.0/include -DNDEBUG -D\\_FORTIFY\\_SOURCE=2 -O2 -isystem /home/aakhmetz/anaconda3/include    -DBOOST\\_DISABLE\\_ASSERTS   -DSTAN\\_MPI  -Wl,-O2 -Wl,--sort-common -Wl,--as-needed -Wl,-z,relro -Wl,-z,now -Wl,--disable-new-dtags -Wl,--gc-sections -Wl,-rpath,/home/aakhmetz/anaconda3/lib -Wl,-rpath-link,/home/aakhmetz/anaconda3/lib -L/home/aakhmetz/anaconda3/lib       -Wl,-L,\"/home/aakhmetz/CmdStan/stan/lib/stan\\_math/lib/boost\\_1.72.0/stage/lib\" -Wl,-rpath,\"/home/aakhmetz/CmdStan/stan/lib/stan\\_math/lib/boost\\_1.72.0/stage/lib\" -Wl,-L,\"/home/aakhmetz/CmdStan/stan/lib/stan\\_math/lib/tbb\" -Wl,-rpath,\"/home/aakhmetz/CmdStan/stan/lib/stan\\_math/lib/tbb\"  ../Dropbox/Hokkaido\\_Wuhan\\_IncubationPeriod\\_2020/scripts/Andrei-more\\_accurate/stan-sims/data\\_incper\\_inclwuhan-weibull-no\\_truncation/fit.o src/cmdstan/main.o         stan/lib/stan\\_math/lib/sundials\\_4.1.0/lib/libsundials\\_nvecserial.a stan/lib/stan\\_math/lib/sundials\\_4.1.0/lib/libsundials\\_cvodes.a stan/lib/stan\\_math/lib/sundials\\_4.1.0/lib/libsundials\\_idas.a stan/lib/stan\\_math/lib/sundials\\_4.1.0/lib/libsundials\\_kinsol.a stan/lib/stan\\_math/lib/boost\\_1.72.0/stage/lib/libboost\\_serialization.so stan/lib/stan\\_math/lib/boost\\_1.72.0/stage/lib/libboost\\_mpi.so stan/lib/stan\\_math/stan/math/prim/functor/mpi\\_cluster\\_inst.o stan/lib/stan\\_math/lib/tbb/libtbb.so.2 -o ../Dropbox/Hokkaido\\_Wuhan\\_IncubationPeriod\\_2020/scripts/Andrei-more\\_accurate/stan-sims/data\\_incper\\_inclwuhan-weibull-no\\_truncation/fit'\n",
       "\\item 'Running 1'\n",
       "\\item 'Running 2'\n",
       "\\item 'Running 3'\n",
       "\\item 'Running 4'\n",
       "\\item 'Finished sampling haha!'\n",
       "\\end{enumerate*}\n"
      ],
      "text/markdown": [
       "1. ''\n",
       "2. '--- Translating Stan model to C++ code ---'\n",
       "3. <span style=white-space:pre-wrap>'bin/stanc  --o=../Dropbox/Hokkaido_Wuhan_IncubationPeriod_2020/scripts/Andrei-more_accurate/stan-sims/data_incper_inclwuhan-weibull-no_truncation/fit.hpp ../Dropbox/Hokkaido_Wuhan_IncubationPeriod_2020/scripts/Andrei-more_accurate/stan-sims/data_incper_inclwuhan-weibull-no_truncation/fit.stan'</span>\n",
       "4. <span style=white-space:pre-wrap>'mpicxx -fvisibility-inlines-hidden -std=c++17 -fmessage-length=0 -march=nocona -mtune=haswell -ftree-vectorize -fPIC -fstack-protector-strong -fno-plt -O2 -ffunction-sections -pipe -isystem /home/aakhmetz/anaconda3/include -std=c++1y -D_REENTRANT -Wno-sign-compare    -Wno-delete-non-virtual-dtor  -I stan/lib/stan_math/lib/tbb_2019_U8/include -O3 -I src -I stan/src -I lib/rapidjson_1.1.0/ -I stan/lib/stan_math/ -I stan/lib/stan_math/lib/eigen_3.3.3 -I stan/lib/stan_math/lib/boost_1.72.0 -I stan/lib/stan_math/lib/sundials_4.1.0/include -DNDEBUG -D_FORTIFY_SOURCE=2 -O2 -isystem /home/aakhmetz/anaconda3/include    -DBOOST_DISABLE_ASSERTS   -DSTAN_MPI   -c -MT ../Dropbox/Hokkaido_Wuhan_IncubationPeriod_2020/scripts/Andrei-more_accurate/stan-sims/data_incper_inclwuhan-weibull-no_truncation/fit.o -MT ../Dropbox/Hokkaido_Wuhan_IncubationPeriod_2020/scripts/Andrei-more_accurate/stan-sims/data_incper_inclwuhan-weibull-no_truncation/fit -include ../Dropbox/Hokkaido_Wuhan_IncubationPeriod_2020/scripts/Andrei-more_accurate/stan-sims/data_incper_inclwuhan-weibull-no_truncation/fit.hpp -include src/cmdstan/main.cpp -MM -E -MG -MP -MF ../Dropbox/Hokkaido_Wuhan_IncubationPeriod_2020/scripts/Andrei-more_accurate/stan-sims/data_incper_inclwuhan-weibull-no_truncation/fit.d ../Dropbox/Hokkaido_Wuhan_IncubationPeriod_2020/scripts/Andrei-more_accurate/stan-sims/data_incper_inclwuhan-weibull-no_truncation/fit.hpp'</span>\n",
       "5. ''\n",
       "6. '--- Compiling, linking C++ code ---'\n",
       "7. <span style=white-space:pre-wrap>'mpicxx -fvisibility-inlines-hidden -std=c++17 -fmessage-length=0 -march=nocona -mtune=haswell -ftree-vectorize -fPIC -fstack-protector-strong -fno-plt -O2 -ffunction-sections -pipe -isystem /home/aakhmetz/anaconda3/include -std=c++1y -D_REENTRANT -Wno-sign-compare    -Wno-delete-non-virtual-dtor  -I stan/lib/stan_math/lib/tbb_2019_U8/include -O3 -I src -I stan/src -I lib/rapidjson_1.1.0/ -I stan/lib/stan_math/ -I stan/lib/stan_math/lib/eigen_3.3.3 -I stan/lib/stan_math/lib/boost_1.72.0 -I stan/lib/stan_math/lib/sundials_4.1.0/include -DNDEBUG -D_FORTIFY_SOURCE=2 -O2 -isystem /home/aakhmetz/anaconda3/include    -DBOOST_DISABLE_ASSERTS   -DSTAN_MPI   -c  -x c++ -o ../Dropbox/Hokkaido_Wuhan_IncubationPeriod_2020/scripts/Andrei-more_accurate/stan-sims/data_incper_inclwuhan-weibull-no_truncation/fit.o ../Dropbox/Hokkaido_Wuhan_IncubationPeriod_2020/scripts/Andrei-more_accurate/stan-sims/data_incper_inclwuhan-weibull-no_truncation/fit.hpp'</span>\n",
       "8. <span style=white-space:pre-wrap>'mpicxx -fvisibility-inlines-hidden -std=c++17 -fmessage-length=0 -march=nocona -mtune=haswell -ftree-vectorize -fPIC -fstack-protector-strong -fno-plt -O2 -ffunction-sections -pipe -isystem /home/aakhmetz/anaconda3/include -std=c++1y -D_REENTRANT -Wno-sign-compare    -Wno-delete-non-virtual-dtor  -I stan/lib/stan_math/lib/tbb_2019_U8/include -O3 -I src -I stan/src -I lib/rapidjson_1.1.0/ -I stan/lib/stan_math/ -I stan/lib/stan_math/lib/eigen_3.3.3 -I stan/lib/stan_math/lib/boost_1.72.0 -I stan/lib/stan_math/lib/sundials_4.1.0/include -DNDEBUG -D_FORTIFY_SOURCE=2 -O2 -isystem /home/aakhmetz/anaconda3/include    -DBOOST_DISABLE_ASSERTS   -DSTAN_MPI  -Wl,-O2 -Wl,--sort-common -Wl,--as-needed -Wl,-z,relro -Wl,-z,now -Wl,--disable-new-dtags -Wl,--gc-sections -Wl,-rpath,/home/aakhmetz/anaconda3/lib -Wl,-rpath-link,/home/aakhmetz/anaconda3/lib -L/home/aakhmetz/anaconda3/lib       -Wl,-L,\"/home/aakhmetz/CmdStan/stan/lib/stan_math/lib/boost_1.72.0/stage/lib\" -Wl,-rpath,\"/home/aakhmetz/CmdStan/stan/lib/stan_math/lib/boost_1.72.0/stage/lib\" -Wl,-L,\"/home/aakhmetz/CmdStan/stan/lib/stan_math/lib/tbb\" -Wl,-rpath,\"/home/aakhmetz/CmdStan/stan/lib/stan_math/lib/tbb\"  ../Dropbox/Hokkaido_Wuhan_IncubationPeriod_2020/scripts/Andrei-more_accurate/stan-sims/data_incper_inclwuhan-weibull-no_truncation/fit.o src/cmdstan/main.o         stan/lib/stan_math/lib/sundials_4.1.0/lib/libsundials_nvecserial.a stan/lib/stan_math/lib/sundials_4.1.0/lib/libsundials_cvodes.a stan/lib/stan_math/lib/sundials_4.1.0/lib/libsundials_idas.a stan/lib/stan_math/lib/sundials_4.1.0/lib/libsundials_kinsol.a stan/lib/stan_math/lib/boost_1.72.0/stage/lib/libboost_serialization.so stan/lib/stan_math/lib/boost_1.72.0/stage/lib/libboost_mpi.so stan/lib/stan_math/stan/math/prim/functor/mpi_cluster_inst.o stan/lib/stan_math/lib/tbb/libtbb.so.2 -o ../Dropbox/Hokkaido_Wuhan_IncubationPeriod_2020/scripts/Andrei-more_accurate/stan-sims/data_incper_inclwuhan-weibull-no_truncation/fit'</span>\n",
       "9. 'Running 1'\n",
       "10. 'Running 2'\n",
       "11. 'Running 3'\n",
       "12. 'Running 4'\n",
       "13. 'Finished sampling haha!'\n",
       "\n",
       "\n"
      ],
      "text/plain": [
       " [1] \"\"                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              \n",
       " [2] \"--- Translating Stan model to C++ code ---\"                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    \n",
       " [3] \"bin/stanc  --o=../Dropbox/Hokkaido_Wuhan_IncubationPeriod_2020/scripts/Andrei-more_accurate/stan-sims/data_incper_inclwuhan-weibull-no_truncation/fit.hpp ../Dropbox/Hokkaido_Wuhan_IncubationPeriod_2020/scripts/Andrei-more_accurate/stan-sims/data_incper_inclwuhan-weibull-no_truncation/fit.stan\"                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         \n",
       " [4] \"mpicxx -fvisibility-inlines-hidden -std=c++17 -fmessage-length=0 -march=nocona -mtune=haswell -ftree-vectorize -fPIC -fstack-protector-strong -fno-plt -O2 -ffunction-sections -pipe -isystem /home/aakhmetz/anaconda3/include -std=c++1y -D_REENTRANT -Wno-sign-compare    -Wno-delete-non-virtual-dtor  -I stan/lib/stan_math/lib/tbb_2019_U8/include -O3 -I src -I stan/src -I lib/rapidjson_1.1.0/ -I stan/lib/stan_math/ -I stan/lib/stan_math/lib/eigen_3.3.3 -I stan/lib/stan_math/lib/boost_1.72.0 -I stan/lib/stan_math/lib/sundials_4.1.0/include -DNDEBUG -D_FORTIFY_SOURCE=2 -O2 -isystem /home/aakhmetz/anaconda3/include    -DBOOST_DISABLE_ASSERTS   -DSTAN_MPI   -c -MT ../Dropbox/Hokkaido_Wuhan_IncubationPeriod_2020/scripts/Andrei-more_accurate/stan-sims/data_incper_inclwuhan-weibull-no_truncation/fit.o -MT ../Dropbox/Hokkaido_Wuhan_IncubationPeriod_2020/scripts/Andrei-more_accurate/stan-sims/data_incper_inclwuhan-weibull-no_truncation/fit -include ../Dropbox/Hokkaido_Wuhan_IncubationPeriod_2020/scripts/Andrei-more_accurate/stan-sims/data_incper_inclwuhan-weibull-no_truncation/fit.hpp -include src/cmdstan/main.cpp -MM -E -MG -MP -MF ../Dropbox/Hokkaido_Wuhan_IncubationPeriod_2020/scripts/Andrei-more_accurate/stan-sims/data_incper_inclwuhan-weibull-no_truncation/fit.d ../Dropbox/Hokkaido_Wuhan_IncubationPeriod_2020/scripts/Andrei-more_accurate/stan-sims/data_incper_inclwuhan-weibull-no_truncation/fit.hpp\"                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          \n",
       " [5] \"\"                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              \n",
       " [6] \"--- Compiling, linking C++ code ---\"                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           \n",
       " [7] \"mpicxx -fvisibility-inlines-hidden -std=c++17 -fmessage-length=0 -march=nocona -mtune=haswell -ftree-vectorize -fPIC -fstack-protector-strong -fno-plt -O2 -ffunction-sections -pipe -isystem /home/aakhmetz/anaconda3/include -std=c++1y -D_REENTRANT -Wno-sign-compare    -Wno-delete-non-virtual-dtor  -I stan/lib/stan_math/lib/tbb_2019_U8/include -O3 -I src -I stan/src -I lib/rapidjson_1.1.0/ -I stan/lib/stan_math/ -I stan/lib/stan_math/lib/eigen_3.3.3 -I stan/lib/stan_math/lib/boost_1.72.0 -I stan/lib/stan_math/lib/sundials_4.1.0/include -DNDEBUG -D_FORTIFY_SOURCE=2 -O2 -isystem /home/aakhmetz/anaconda3/include    -DBOOST_DISABLE_ASSERTS   -DSTAN_MPI   -c  -x c++ -o ../Dropbox/Hokkaido_Wuhan_IncubationPeriod_2020/scripts/Andrei-more_accurate/stan-sims/data_incper_inclwuhan-weibull-no_truncation/fit.o ../Dropbox/Hokkaido_Wuhan_IncubationPeriod_2020/scripts/Andrei-more_accurate/stan-sims/data_incper_inclwuhan-weibull-no_truncation/fit.hpp\"                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            \n",
       " [8] \"mpicxx -fvisibility-inlines-hidden -std=c++17 -fmessage-length=0 -march=nocona -mtune=haswell -ftree-vectorize -fPIC -fstack-protector-strong -fno-plt -O2 -ffunction-sections -pipe -isystem /home/aakhmetz/anaconda3/include -std=c++1y -D_REENTRANT -Wno-sign-compare    -Wno-delete-non-virtual-dtor  -I stan/lib/stan_math/lib/tbb_2019_U8/include -O3 -I src -I stan/src -I lib/rapidjson_1.1.0/ -I stan/lib/stan_math/ -I stan/lib/stan_math/lib/eigen_3.3.3 -I stan/lib/stan_math/lib/boost_1.72.0 -I stan/lib/stan_math/lib/sundials_4.1.0/include -DNDEBUG -D_FORTIFY_SOURCE=2 -O2 -isystem /home/aakhmetz/anaconda3/include    -DBOOST_DISABLE_ASSERTS   -DSTAN_MPI  -Wl,-O2 -Wl,--sort-common -Wl,--as-needed -Wl,-z,relro -Wl,-z,now -Wl,--disable-new-dtags -Wl,--gc-sections -Wl,-rpath,/home/aakhmetz/anaconda3/lib -Wl,-rpath-link,/home/aakhmetz/anaconda3/lib -L/home/aakhmetz/anaconda3/lib       -Wl,-L,\\\"/home/aakhmetz/CmdStan/stan/lib/stan_math/lib/boost_1.72.0/stage/lib\\\" -Wl,-rpath,\\\"/home/aakhmetz/CmdStan/stan/lib/stan_math/lib/boost_1.72.0/stage/lib\\\" -Wl,-L,\\\"/home/aakhmetz/CmdStan/stan/lib/stan_math/lib/tbb\\\" -Wl,-rpath,\\\"/home/aakhmetz/CmdStan/stan/lib/stan_math/lib/tbb\\\"  ../Dropbox/Hokkaido_Wuhan_IncubationPeriod_2020/scripts/Andrei-more_accurate/stan-sims/data_incper_inclwuhan-weibull-no_truncation/fit.o src/cmdstan/main.o         stan/lib/stan_math/lib/sundials_4.1.0/lib/libsundials_nvecserial.a stan/lib/stan_math/lib/sundials_4.1.0/lib/libsundials_cvodes.a stan/lib/stan_math/lib/sundials_4.1.0/lib/libsundials_idas.a stan/lib/stan_math/lib/sundials_4.1.0/lib/libsundials_kinsol.a stan/lib/stan_math/lib/boost_1.72.0/stage/lib/libboost_serialization.so stan/lib/stan_math/lib/boost_1.72.0/stage/lib/libboost_mpi.so stan/lib/stan_math/stan/math/prim/functor/mpi_cluster_inst.o stan/lib/stan_math/lib/tbb/libtbb.so.2 -o ../Dropbox/Hokkaido_Wuhan_IncubationPeriod_2020/scripts/Andrei-more_accurate/stan-sims/data_incper_inclwuhan-weibull-no_truncation/fit\"\n",
       " [9] \"Running 1\"                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     \n",
       "[10] \"Running 2\"                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     \n",
       "[11] \"Running 3\"                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     \n",
       "[12] \"Running 4\"                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     \n",
       "[13] \"Finished sampling haha!\"                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       "
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "idx = length(filenames)\n",
    "{\n",
    "    ## main dir for Stan simulations\n",
    "    standirname = 'stan-sims/'%&%filenames[idx]%&%\"-weibull-no_truncation\"\n",
    "    unlink(standirname, recursive=T)\n",
    "    dir.create(standirname)\n",
    "\n",
    "    datafilename = \"../../data/\"%&%filenames[idx]%&%\".csv\"\n",
    "    read.table(datafilename, sep=\",\", header=T) %>% select(EL,ER,SL,SR,tstar) %>% mutate(dist = (SR+SL)/2-(ER+EL)/2) -> df -> df\n",
    "    df %>% filter(EL>0) -> df1 # E_L is defined\n",
    "    df %>% filter(EL==0) -> df2 # E_L is missing\n",
    "    df = rbind(df1,df2) # we move all incomplete records to the end of the dataframe\n",
    "\n",
    "    # Dumping data\n",
    "    N = nrow(df)\n",
    "    N2 = nrow(df2)\n",
    "    N1 = N - N2\n",
    "    E_L = df$EL\n",
    "    E_R = df$ER\n",
    "    S_L = df$SL\n",
    "    S_R = df$SR\n",
    "    stan_rdump(c('E_L', 'E_R', 'S_L', 'S_R', 'N', 'N2'), file=standirname%&%\"/Data.R\") \n",
    "  \n",
    "    # Dumping initial conditions\n",
    "    logmean_SI = log(mean(df$dist))\n",
    "    param1 = 1.75\n",
    "    s_raw = rep(.6, N)\n",
    "    e_raw = rep(.4, N1)\n",
    "    t = rep(5.0, N2)\n",
    "    E_L_est_raw = rep(.5, N2)\n",
    "    stan_rdump(c('t', 'E_L_est_raw', 's_raw', 'e_raw', 'logmean_SI', 'param1'), file=standirname%&%\"/Init.R\")   \n",
    "\n",
    "    # Stan program\n",
    "    \"data {\n",
    "        int<lower = 0> N; // number of records\n",
    "        int<lower = 0> N2; // number of incomplete records\n",
    "        vector<lower = 0>[N] E_L;\n",
    "        vector<lower = 0>[N] E_R;\n",
    "        vector<lower = 0>[N] S_L;\n",
    "        vector<lower = 0>[N] S_R;\n",
    "    }\n",
    "\n",
    "    transformed data {\n",
    "        int<lower = 0> N1 = N - N2;\n",
    "    }\n",
    "\n",
    "    parameters {\n",
    "        real<lower = 0> mean_SI;\n",
    "        real<lower = 0> param1;\n",
    "\n",
    "        vector<lower = 0, upper = 1>[N] s_raw;\n",
    "        vector<lower = 0, upper = 1>[N1] e_raw;\n",
    "        vector<lower = 0>[N2] t;\n",
    "        vector<lower = 0, upper = 1>[N2] E_L_est_raw;\n",
    "    }\n",
    "\n",
    "    transformed parameters {\n",
    "        real<lower = 0> param2 = mean_SI/tgamma(1.0+1.0/param1);\n",
    "\n",
    "        vector<lower = min(S_L), upper = max(S_R)>[N] s;\n",
    "        vector<lower = min(E_L), upper = max(S_R)>[N1] e;\n",
    "        vector<lower = 0, upper = max(E_R)>[N2] E_L_est;\n",
    "\n",
    "        s = S_L + (S_R - S_L) .* s_raw;\n",
    "        for (k in 1:N1) {\n",
    "            if (E_R[k] > s[k]) \n",
    "                e[k] = E_L[k] + (s[k] - E_L[k]) * e_raw[k];\n",
    "            else\n",
    "                e[k] = E_L[k] + (E_R[k] - E_L[k]) * e_raw[k];\n",
    "        }\n",
    "\n",
    "        for (m in 1:N2) \n",
    "            E_L_est[m] = E_R[N1 + m] * E_L_est_raw[m];\n",
    "    }\n",
    "\n",
    "    model {\n",
    "        mean_SI ~ normal(5.0, 10.0);\n",
    "        param1 ~ exponential(0.0001);\n",
    "\n",
    "        e_raw ~ normal(0.5, 1.0);\n",
    "        s_raw ~ normal(0.5, 1.0);\n",
    "        t ~ normal(5.0, 10.0);\n",
    "        E_L_est_raw ~ normal(0.5, 1.0);\n",
    "\n",
    "        for (k in 1:N1) \n",
    "            target += weibull_lpdf(s[k] - e[k] | param1, param2);\n",
    "\n",
    "        for (m in 1:N2) {\n",
    "            target += weibull_lpdf(t[m] | param1, param2) + weibull_lcdf(s[N1 + m] - E_L_est[m] | param1, param2);\n",
    "            if (s[N1 + m] > E_R[N1 + m])\n",
    "                target += weibull_lccdf(s[N1 + m] - E_R[N1 + m] | param1, param2);       \n",
    "        }\n",
    "    }\n",
    "\n",
    "    generated quantities {\n",
    "        real sd_SI = param2*sqrt(tgamma(1.0+2.0/param1)-(tgamma(1.0+1.0/param1))^2);\n",
    "\n",
    "        vector[N] log_likelihood;\n",
    "        for (k in 1:N1) \n",
    "            log_likelihood[k] = weibull_lpdf(s[k] - e[k] | param1, param2);\n",
    "\n",
    "        for (m in 1:N2) {\n",
    "            log_likelihood[N1 + m] = weibull_lpdf(t[m] | param1, param2) + weibull_lcdf(s[N1 + m] - E_L_est[m] | param1, param2);\n",
    "            if (s[N1 + m] > E_R[N1 + m])\n",
    "                log_likelihood[N1 + m] += weibull_lccdf(s[N1 + m] - E_R[N1 + m] | param1, param2);       \n",
    "        }\n",
    "    }\" %>% cat(file=standirname %&% \"/fit.stan\", sep=\"\", fill=TRUE)\n",
    "\n",
    "    standistribdir = \"../../../../CmdStan\"\n",
    "    stanscriptdir = \"../Dropbox/Hokkaido_Wuhan_IncubationPeriod_2020/scripts/Andrei-more_accurate/\"%&%standirname\n",
    "    ## bash file\n",
    "    \"#!/bin/bash\n",
    "    cwd=$(pwd)\n",
    "    cd \"%&%standistribdir%&%\"\n",
    "    make -j6 \"%&%stanscriptdir%&%\"/fit\n",
    "    cd \"%&%stanscriptdir%&%\"\n",
    "    mkdir -p diagnostics\n",
    "    for i in {1..4}\n",
    "    do\n",
    "        echo Running ${i}\n",
    "        SEEDNUMBER=$((1+$i))\n",
    "        ./fit \\\\\n",
    "            method=sample num_samples=10000 num_warmup=10000 save_warmup=0 \\\\\n",
    "                adapt delta=0.98 \\\\\n",
    "                algorithm=hmc \\\\\n",
    "                    engine=nuts \\\\\n",
    "            random seed=${SEEDNUMBER} \\\\\n",
    "            id=$i \\\\\n",
    "            data file=Data.R \\\\\n",
    "            init=Init.R \\\\\n",
    "            output file=trace-$i.csv \\\\\n",
    "                diagnostic_file=diagnostics/diagnostics-$i.csv > diagnostics/output-$i.txt &\n",
    "    done\n",
    "    echo Finished sampling haha!\n",
    "    \" %>% cat(file=standirname%&%\"/fit.sh\", sep=\"\", fill=TRUE)\n",
    "\n",
    "    ## running the bash script\n",
    "    system(\"bash \"%&%standirname%&%\"/fit.sh\", intern = TRUE)\n",
    "}"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "R",
   "language": "R",
   "name": "ir"
  },
  "language_info": {
   "codemirror_mode": "r",
   "file_extension": ".r",
   "mimetype": "text/x-r-source",
   "name": "R",
   "pygments_lexer": "r",
   "version": "3.6.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
